停止运行
Stop a Runnable
在我之前的问题 中,我找到了这样的答案:
Handler visibilityToggler;
Runnable visivilityRunnable;
visibilityToggler = new Handler();
visivilityRunnable = new Runnable() {
@Override
public void run() {
// isUserClickedButton is used to keep record if user has pressed button within 1 sec
// keep isUserClickedButton = true for first time as it will run
if (!isUserClickedButton) {
// user not pressed button
Toast.makeText(context,"You are not pressed the Button",Toast.LENGHT_LONG).show();
}
// toggle visibility
Random generator = new Random();
number = generator.nextInt(16);
for (int i = 0; i < buttons.length; i++) {
if (i == number)
buttons[i].setVisibility(View.VISIBLE);
else
buttons[i].setVisibility(View.INVISIBLE);
}
// again start the visibility
visibilityToggler.postDelayed(visivilityRunnable,1000);
// make it false as visibility is toggled and we want to track button pressed from start
isUserClickedButton = false;
}
};
visibilityToggler.postDelayed(visivilityRunnable,1000);
现在我的问题是:如何停止 Runnable?
谁能给个代码示例?
更新:
我在按钮上使用了 removecallbacks()
public void onClick(View v){
visibilityToggler.removeCallbacks(visivilityRunnable);
}
但是runnable没有停止,有人可以帮忙吗?
在处理程序外定义可运行对象。然后在要停止处理程序的位置调用 handler.removeCallbacks(visivilityRunnable);
。
我不确定你是否想打破循环,但如果我理解你的话,我会建议你创建一个静态布尔变量并在需要时停止它
public static boolean isWorking=true;
for (int i = 0; i < buttons.length; i++) {
if(isWork){
if (i == number)
buttons[i].setVisibility(View.VISIBLE);
else
buttons[i].setVisibility(View.INVISIBLE);
}else break;}
现在,如果您想打破循环,只需将 isWork
更改为 false
在我之前的问题
Handler visibilityToggler;
Runnable visivilityRunnable;
visibilityToggler = new Handler();
visivilityRunnable = new Runnable() {
@Override
public void run() {
// isUserClickedButton is used to keep record if user has pressed button within 1 sec
// keep isUserClickedButton = true for first time as it will run
if (!isUserClickedButton) {
// user not pressed button
Toast.makeText(context,"You are not pressed the Button",Toast.LENGHT_LONG).show();
}
// toggle visibility
Random generator = new Random();
number = generator.nextInt(16);
for (int i = 0; i < buttons.length; i++) {
if (i == number)
buttons[i].setVisibility(View.VISIBLE);
else
buttons[i].setVisibility(View.INVISIBLE);
}
// again start the visibility
visibilityToggler.postDelayed(visivilityRunnable,1000);
// make it false as visibility is toggled and we want to track button pressed from start
isUserClickedButton = false;
}
};
visibilityToggler.postDelayed(visivilityRunnable,1000);
现在我的问题是:如何停止 Runnable?
谁能给个代码示例?
更新:
我在按钮上使用了 removecallbacks()
public void onClick(View v){
visibilityToggler.removeCallbacks(visivilityRunnable);
}
但是runnable没有停止,有人可以帮忙吗?
在处理程序外定义可运行对象。然后在要停止处理程序的位置调用 handler.removeCallbacks(visivilityRunnable);
。
我不确定你是否想打破循环,但如果我理解你的话,我会建议你创建一个静态布尔变量并在需要时停止它
public static boolean isWorking=true;
for (int i = 0; i < buttons.length; i++) {
if(isWork){
if (i == number)
buttons[i].setVisibility(View.VISIBLE);
else
buttons[i].setVisibility(View.INVISIBLE);
}else break;}
现在,如果您想打破循环,只需将 isWork
更改为 false