I tried something that works but could there be a better way to do it?
private Boolean set = false;
public void btnClick(View view) {
set = ! set;
Button button = findViewById(R.id.button);
if(set == true) {button.setText("Hi!");}
else{button.setText("Hi again!");}
}
Just simply do
button.setText(set? "Hi" : "Hi again");
The method has parameter as a View class. Which means you simply do not need to define the button.
private Boolean set = false;
public void btnClick(View view) {
set = ! set;
//Then the below line of code can be written somewhat like the above user mentioned. Changing a minor detail.
view.setText(set ? "Hi" : "Hi again");
}
And you can mention this method in layout as button's onClick
property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.