如何添加一个将我的两个计数器都重启为0的按钮?

时间:2014-11-05 23:27:40

标签: java android button counter restart

我想添加一个按钮,当我按下它时,它会将两个文本视图重新启动回0.我该怎么做?先感谢您。我非常感谢你的帮助。

final TextView text = (TextView) findViewById(R.id.textView2);
text.setText("");
final ImageButton button1 = (ImageButton)findViewById(R.id.imageButton2);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        clicked1++;
        text.setText("  " + clicked1 + " SHOTS ");
    }

});
}

final TextView text = (TextView) findViewById(R.id.textView1);
text.setText("");
final ImageButton button2 = (ImageButton)findViewById(R.id.imageButton1);
button2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        clicked2++;
        text.setText("  " + clicked2 + " CUPS ");
    }

});

}
}

2 个答案:

答案 0 :(得分:1)

您必须在第三个按钮的onClick中清除两个计数器,并使用新清除的计数器(零)设置两个文本视图。

final ImageButton buttonClearCounter = (ImageButton)findViewById(R.id.imageButton);
    button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        clicked1 = 0;
        clicked2 = 0;
        text1.setText("  " + clicked1 + " SHOTS");
        text2.setText("  " + clicked2 + " CUPS");

    }

答案 1 :(得分:-1)

    {
        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                    clicked1 = 0;
                    clicked2 = 0;
                    text1.setText("  " + clicked1 + " SHOTS");
                    text2.setText("  " + clicked2 + " CUPS");

            }
        });
    }