简体   繁体   中英

Button click change textview

I want to make a simple counter program where I have a Textview and two buttons, one with + another with - whichever I press I get 1 incremented or decremented.

My problem is that I want the Textview to update a variable and not text. (By that I mean I get the app to work with .setText("counter:"+ var_counter) but doesn't work with .setText(var_counter) .

var_counter is an int.

    final TextView tvContador = (TextView)findViewById(R.id.tvContador);
Button add = (Button)findViewById(R.id.bMais);
Button sub = (Button)findViewById(R.id.bMenos);
add.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           contador++;                                 
           tvContador.setText(getText(contador));
       }
});

ps: my string is a resource string.

Since var_counter is an int , setText(var_counter) tries to set the text of the widget to the string resource with such value as its id . What you want is:

setText(Integer.toString(var_counter));

采用

.setText(Integer.toString(var_counter));

只是:

.setText(""+var_counter);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM