[FIXED] Simpler way to create multiple appends for a custom numpad

Issue

is there a faster/simpler way to make this:? I’m creating a numpad in my Android App which I use 10 buttons for and a delete button. Right now I’m doing it like this but like this it’s gonna take a lot of code.

    num0.setOnClickListener(){
        editTextNumberPassword.append("0")
    }
    num1.setOnClickListener(){
        editTextNumberPassword.append("1")
    }
    num2.setOnClickListener(){
        editTextNumberPassword.append("3")
    }

Solution

Add all views into a list and then use the index:

listOf(num0, num1, num2)
    .withIndex()
    .forEach { (index, view) ->
        view.setOnClickListener { editTextNumberPassword.append("$index") }
    }

Answered By – Some random IT boy

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published