I've been trying to change a buttons' text with each click and have had success the first time the button has clicked. Have been playing around with button.setTag and button.getTag to try to transition through the state of the button clicks.
Exactly what I want to do...
Button defaults to " " at start First click text goes to "X" Second click text goes to "O" Third click text goes to " " //did not try to enable this yet
Here is my code so far..
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button button = (Button) findViewById(R.id.button);
button.setTag(0);
button.setText("");
final int status = (Integer) view.getTag();
switch (status) {
case 0:
button.setText("X");
view.setTag(1); //pause
break;
case 1:
button.setText("O");
view.setTag(0); //pause
break;
}
}
});//if you know why android studio makes me add this get bonus points
}
Currently everytime you click the button you set the buttons tag to 0:
button.setTag(0);
change to this:
if(button.getTag() == null){
button.setTag(0);
}
and it should work.
int count = 2;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button button = (Button) findViewById(R.id.button);
count++;
if(count%3==0)button.setText("X");
else if(count%3==1)button.setText("O");
else button.setText("");
}
}
});
}
How about this? Also, I'd recommend casting all your widgets in onCreate so you only have to do it once per lifecycle.
Here is your issue:
button.setTag(0);
button.setText("");
final int status = (Integer) view.getTag();
so status will always be 0
.
Change your method to this:
button.setTag(0);//do this when you first initialize the button. You can even specify this in Xml with android:tag="0"
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button button = (Button) findViewById(R.id.button);
button.setText("");
final int status = (Integer) view.getTag();
switch (status) {
case 0:
button.setText("X");
view.setTag(1); //pause
break;
case 1:
button.setText("O");
view.setTag(0); //pause
break;
}
}
});
I also recommend changing tags to static variables for clarity. To do this, declare these class constants:
public static final int STATE_PAUSED = 0;
public static final int STATE_UNPAUSED = 1;
Now you can set the tag with the commands:
view.setTag(STATE_PAUSED);
or
view.setTag(STATE_UNPAUSED);
button.setTag(0); button.setText("");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button button = (Button) findViewById(R.id.button);
button.setText("");
final int status = (Integer) view.getTag();
switch (status) {
case 0:
button.setText("X");
view.setTag(1); //pause
break;
case 1:
button.setText("O");
view.setTag(2); //pause
break;
case 2:
button.setText("");
view.setTag(0);
}
}
});
}
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.