'Increment and decrement EditTextInput
is there a way to do this:
var num = 0
plusbtn.setOnClickListener {
num++
textView2.text = num.toString()
}
minbtn.setOnClickListener {
num--
textView2.text = num.toString()
}
but instead of the textView2, I use a inputtype thing. So that I can still type the numbers in it, but also use the 2 buttons to increment en decrement.
Solution 1:[1]
I have made this. This now works as I want it to:
plusbtn.setOnClickListener {
if(isEmpty(penaltytimetxt.text)){
penaltytimetxt.setText("0")
}
num = penaltytimetxt.text.toString().toInt()
if(num < 99){
num++
penaltytimetxt.setText(num.toString())
}
else {
Toast.makeText(this, "Penalty time cannot go over 99 sec", Toast.LENGTH_SHORT).show()
}
}
minbtn.setOnClickListener {
if(isEmpty(penaltytimetxt.text)){
penaltytimetxt.setText("0")
}
num = penaltytimetxt.text.toString().toInt()
if(num > 0){
num--
penaltytimetxt.setText(num.toString())
}
else {
Toast.makeText(this, "Penalty time cannot go under 0 sec", Toast.LENGTH_SHORT).show()
}
}
Solution 2:[2]
in TextInputEditText yo should use setText method
var num = 0
plusbtn.setOnClickListener {
num++
textView2.setText(num.toString())
}
minbtn.setOnClickListener {
num--
textView2.setText(num.toString())
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | hossein |
