Home > OS >  Simpler way to create multiple appends for a custom numpad
Simpler way to create multiple appends for a custom numpad

Time:09-14

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")
    }

CodePudding user response:

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

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