0
我开发一个程序,当我点击一个按钮开始计算点击次数并显示计数时,并且在5秒后该按钮是可以理解的,并且我也想显示倒计时秒数不显示同时在一个按钮上执行三个onClick操作
mTextView = (TextView) findViewById(R.id.total_textview);
mTextView.setVisibility(View.VISIBLE);
Button button = (Button) findViewById(R.id.count_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final Button b = (Button)v;
if (clicks == 0){
// Means its the first time that a user click the button
// Start a thread that is going to disable the button after 5 seconds from first click
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
@Override
public void run() {
b.setText("Time up");
b.setEnabled(false);
// Showing user clicks after button is disabled
showClicks();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
// Here we are just counting . . . . including the first click
countClicks();
}
});
}
private void countClicks(){
++clicks;
mTextView.setText(Integer.toString(clicks));
// You can update your text view here
}
private void showClicks(){
mTextView.setText(String.valueOf(clicks)+"Clicks");
mTextView.setVisibility(View.VISIBLE);
}
}