通过整数变量增加TextView

时间:2016-09-27 21:45:07

标签: android onclick textview integer addition

我有一个带有简单onClick方法的按钮。每次单击按钮时,我希望我的TextView增加1.例如,我的TextView显示0 当单击按钮时,它会显示1,当再次单击该按钮时,它应显示2 ...依此类推。我希望我的观点很明确;)

公共类游戏扩展了活动{

ViewGroup layout;
TextView score1;
String score;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    score1 = (TextView)findViewById(R.id.textView5);

    score = score1.getText().toString();

    layout = (ViewGroup) findViewById(R.id.frameLayout2);
    layout.setOnTouchListener(
            new RelativeLayout.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    String presentValStr=score1.getText().toString();
                    int presentIntVal=Integer.parseInt(presentValStr);
                    presentIntVal++;
                    score1.setText(String.valueOf(presentIntVal));

                    return true;
                }
            }

                );
}

1 个答案:

答案 0 :(得分:0)

private int count=0; 

@Override public void onClick(View v) {
     count++;
     textView.setText(Integer.toString(count));
}