+ 2
How to change text on Button click ?
in android studio I have Button and Textview I need if i press button change text to(this is text 1) and if i press again change to (this is text 2) and if i press again change to (this is text 3 ) Provided that the texts are mixed and not arranged Thanks to everyone who is interested
1 Odpowiedź
+ 10
Button btn = (Button) findViewById(R.id.my_button);
TextView textView = (TextView) findViewById(R.id.my_textview);
int count = 1;
btn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("this is text " + count++);
}
});
Where my_button is the id for your button and my_textview is the id for your TextView in your corresponding layout.xml file.
As far as changing the TextView's text each time the button is pressed goes, more info is needed. Are you storing the text strings in the strings.xml file? Are you going to store them in an array? Are they random? Do they have an actual incrementing number like they do in your example or was that just to show that they change? In the example code above the count increments each time the button is pressed.