Java 在Android上停止for循环的按钮
我想用一个按钮停止for循环,但我无法让它工作Java 在Android上停止for循环的按钮,java,android,button,for-loop,Java,Android,Button,For Loop,我想用一个按钮停止for循环,但我无法让它工作 我有一个布尔值来表示以前没有按下过按钮。 按下按钮时,布尔值更改为指示已按下,启动方法AccionPorSegundo,并将按钮文本更改为“stop” 在AccionPorSegundo中有一个持续40秒的循环 如果按下按钮,此时文本为“停止”,则循环停止 这是我的代码,但不起作用: public class EL_Entrenamiento extends ActionBarActivity implements View.OnClickList
AccionPorSegundo
,并将按钮文本更改为“stop”AccionPorSegundo
中有一个持续40秒的循环public class EL_Entrenamiento extends ActionBarActivity implements View.OnClickListener {
Handler mHandler = new Handler();
TextView tv1,tv2;
int a = 1;
int max = 9;
int min = 0;
int i = 1;
Button comenzar;
Boolean exit = false;
Boolean presionado = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.el_entrenamiento_layout);
comenzar = (Button) findViewById(R.id.bt_el_entrenamiento_comenzar);
tv1 = (TextView) findViewById(R.id.tv_El_entrenamiento1);
tv2 = (TextView) findViewById(R.id.tv_El_entrenamiento2);
comenzar.setOnClickListener(this);
//ACTION BAR PERSON
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.ab_background_textured_rojo);
bg.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
getSupportActionBar().setBackgroundDrawable(bg);
BitmapDrawable bgSplit = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_split_img);
bgSplit.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
getSupportActionBar().setLogo(R.drawable.ic_action_entrenamiento);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//FONFO BOTON PERSON
comenzar.setBackgroundDrawable(bg);
}
final Handler handler = new Handler();
final Timer timer = new Timer();
final Runnable doA = new Runnable() {
@Override
public void run() {
Random rand = new Random();
int randomNum1 = rand.nextInt((max - min) + 1) + min;
int randomNum2 = rand.nextInt((max - min) + 1) + min;
tv1.setVisibility(View.VISIBLE);
tv2.setVisibility(View.VISIBLE);
tv1.setText("" + randomNum1);
tv2.setText("" + randomNum2);
tv1.postDelayed(new Runnable() {
public void run() {
tv1.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.INVISIBLE);
if (a==41){
comenzar.setText("Comenzar");
}
}
}, 500);
a++;
}
};
public void AccionPorSegundo(){
for ( i = 1; i <= 40; i++) {
if (this.exit) break;
TimerTask task = new TimerTask() {
@Override
public void run() {
handler.post(doA);
}
};
timer.schedule(task, i * 1000);
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_el_entrenamiento_comenzar:
if (!presionado){
presionado=true;
AccionPorSegundo();
comenzar.setText("Stop");
}else{
exit=true;
comenzar.setText("Comenzar");
}
}
}
}
公共类EL_Enternamiento扩展ActionBarActivity实现View.OnClickListener{
Handler mHandler=新的Handler();
文本视图tv1、tv2;
INTA=1;
int max=9;
int min=0;
int i=1;
按钮comenzar;
布尔退出=假;
布尔预测=假;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.el_Enternamiento_layout);
comenzar=(按钮)findViewById(R.id.bt_el_Enternamiento_comenzar);
tv1=(文本视图)findViewById(R.id.tv_El_Enternamiento1);
tv2=(TextView)findViewById(R.id.tv_El_Enternamient2);
comenzar.setOnClickListener(本);
//行动酒吧人员
BitmapDrawable bg=(BitmapDrawable)getResources().getDrawable(R.drawable.ab_background_textured_rojo);
setTileModeXY(Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
getSupportActionBar().setBackgroundDrawable(bg);
BitmapDrawable bgSplit=(BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_split_img);
bgSplit.setTileModeXY(Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
getSupportActionBar().setLogo(R.drawable.ic_action_enternamiento);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//方佛波顿人
comenzar.可缩进地面牵引(bg);
}
最终处理程序=新处理程序();
最终计时器=新计时器();
最终可运行doA=新可运行(){
@凌驾
公开募捐{
Random rand=新的Random();
int randomNum1=rand.nextInt((最大-最小)+1)+min;
int randomNum2=rand.nextInt((最大-最小)+1)+min;
tv1.setVisibility(View.VISIBLE);
tv2.setVisibility(View.VISIBLE);
tv1.setText(“+randomNum1”);
tv2.setText(“+randomNum2”);
tv1.postDelayed(新的Runnable(){
公开募捐{
tv1.setVisibility(视图.不可见);
tv2.设置可见性(视图.不可见);
如果(a==41){
comenzar.setText(“comenzar”);
}
}
}, 500);
a++;
}
};
公共无效帐户porsegundo(){
对于(i=1;i您将用for循环开始40个线程。为什么不使用1个线程并在那里完成所有工作?然后您将布尔退出设置为false以停止反循环
for(int i=0; i<40 && exit; i++) {
// do the things you want
}
for(int i=0;imartijnn2008的答案很有帮助,但我遇到了同样的问题,并通过使用“退出”或“退出”解决了它。
我将&&exit
更改为| | exit
,这样就可以分别从这两个位置停止循环:)在调用AccionPorSegundo()后,您打算如何通过将exit
设置为true来停止循环
函数,当您使退出=true
时,什么能够阻止该函数完全执行?很抱歉,我忘记了代码中的一行,它现在正在更新。@Shobhitpuri我正在尝试您的答复,但仍然无法使其工作,线程没有停止