Increment TextView with button click

后端 未结 2 414
悲&欢浪女
悲&欢浪女 2020-12-22 07:24

thanks for the help but I\'m still struggling. I did this:

Button in xml:

相关标签:
2条回答
  • 2020-12-22 08:01

    I cant actually figure out what the increment textview means ....

    May be adding more textviews or increasing the value inside the textview. I will go with later.

    Context context;
    TextView tv;
    Button increamenter;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.my_layout);
       tv=(TextView)findViewById(R.id.textView1);
       incrementer=(Button)findViewById(R.id.button1);
    
        context=this;
    
       incrementer.setOnClickListener(new View.OnClickListener(){
    
            try
            {
                String presentValStr=tv.getText().toString();
                int presentIntVal=Integer.parseInt(presentValStr);
                presentIntVal++;
                tv.setText(String.valueOf(presentIntVal));
             }
             catch(Exception e)
             {
                 e.printStackTrace();
                 Toast.makeText(context,"Some error :(",2000).show();
             }
       });
    
    
    }
    
    0 讨论(0)
  • 2020-12-22 08:17

    In your class create:

           int score = 0;
    

    And then;

           button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // score operation like score += increment_value;
                    t1.setText(""+score);
                }
            });
    

    If you need to "save" the score ocne you quit the application, you need to use SharedPreferences to show/update the value.

    0 讨论(0)
提交回复
热议问题