我正在android studio中开发一个程序。我想当我点击一个按钮然后动作将同时执行。 功能: 1st - 开始计算点击次数 第二 - 5秒后该按钮禁用
setDelay = new Handler();
btn = (Button) findViewById(R.id.bt);
final Button disableMe = (Button) findViewById(R.id.bt);
final TextView text = (TextView) findViewById(R.id.timeUp);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
startDelay = new Runnable(){
@Override
public void run(){
text.setText("Time Up!");
disableMe.setEnabled(false);
}
};
setDelay.postDelayed(startDelay, 5000);
}
});
txv = (TextView) findViewById(R.id.tx);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCounter ++;
txv.setText(Integer.toString(mCounter));
}
});
答案 0 :(得分:0)
像这样创建新列表:
ArrayList <View.OnClickListener> clickList = new ArrayList<>();
然后添加所有听众:
final Runnable startDelay;
clickList.add(new View.OnClickListener(){
@Override
public void onClick(View v){
if (startDelay != null)
return;
startDelay = new Runnable(){
@Override
public void run(){
text.setText("Time Up!");
disableMe.setEnabled(false);
}
};
setDelay.postDelayed(startDelay, 5000);
}
});
clickList.add(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCounter ++;
txv.setText(Integer.toString(mCounter));
}
});
然后,在你的按钮中添加这个监听器:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (View.OnClickListener c : clickList)
c.onClick(view);
}
});
答案 1 :(得分:0)
只需使用class MyClass extends AnotherClass {
public $course_id = 0;
}
变量来判断是否已启用禁用boolean
:
Runnable
将button.setOnClickListener(new View.OnClickListener() {
boolean disableRunnableStarted = false;
Runnable disableRunnable = new Runnable(){
@Override
public void run(){
//Running on UI thread to update button
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
button.setEnabled(false);
}
});
}
};
@Override
public void onClick(View v) {
if(!disableRunnableStarted) {
Log.d(TAG,"Starting disable runnable and incrementing counter...");
new Handler().postDelayed(disableRunnable,5000);
disableRunnableStarted = true;
//Here increment your counter
} else {
Log.d(TAG,"Just incrementing counter...");
//Here increment your counter
}
}
});
替换为MainActivity
的名称,如果Activity
代替Fragment
执行。
答案 2 :(得分:0)
这可以帮助
private int clicks = 0;
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;
// You can update your text view here
}
显示总点击次数
private void showClicks(){
Toast.makeText(this, Integer.toString(clicks), Toast.LENGTH_SHORT).show();
}