有更好的方法吗?这是我正在制作的凯格尔应用程序,当他们变老时,没有人想要滴水,对吧?!我试过在for循环中使用它,但它只循环一次。 :(现在我只是输入更多,因为该网站不会让我发布,因为它说它看起来像我的帖子主要是代码。
public final void onClick(View view) {
switch (view.getId()) {
case R.id.tvBeginner:
tvBeginner.setText("Beginner exercise in progress...");
Runnable clenchRunnable = new Runnable() {
@Override
public void run() {
try {
tvExpert.setText("");
tvIntermediate.setText("Clench!!!");
tvIntermediate.setTextSize(60);
vibrate.vibrate(250);
clenchDone = true;
} catch (Exception e) {
e.printStackTrace();
}
}
};
Runnable relaxRunnable = new Runnable() {
@Override
public void run() {
try {
tvIntermediate.setText("");
tvExpert.setText("Relax...");
tvExpert.setTextSize(40);
tvExpert.setTextColor(Color.GREEN);
vibrate.vibrate(250);
clenchDone = false;
} catch (Exception e) {
e.printStackTrace();
}
}
};
clenchHandler.postDelayed(clenchRunnable, 1000);
relaxHandler.postDelayed(relaxRunnable, 2000);
clenchHandler.postDelayed(clenchRunnable, 3000);
relaxHandler.postDelayed(relaxRunnable, 4000);
clenchHandler.postDelayed(clenchRunnable, 5000);
relaxHandler.postDelayed(relaxRunnable, 6000);
clenchHandler.postDelayed(clenchRunnable, 7000);
relaxHandler.postDelayed(relaxRunnable, 8000);
clenchHandler.postDelayed(clenchRunnable, 9000);
relaxHandler.postDelayed(relaxRunnable, 10000);
clenchHandler.postDelayed(clenchRunnable, 11000);
relaxHandler.postDelayed(relaxRunnable, 12000);
clenchHandler.postDelayed(clenchRunnable, 13000);
relaxHandler.postDelayed(relaxRunnable, 14000);
clenchHandler.postDelayed(clenchRunnable, 15000);
relaxHandler.postDelayed(relaxRunnable, 16000);
clenchHandler.postDelayed(clenchRunnable, 17000);
relaxHandler.postDelayed(relaxRunnable, 18000);
clenchHandler.postDelayed(clenchRunnable, 19000);
relaxHandler.postDelayed(relaxRunnable, 20000);
break;
答案 0 :(得分:0)
你可以在各自的runnables中调用relaxHandler,clenchHandler并保持你想要调用它的次数的计数器即:
int i =0;
Runnable relaxRunnable = new Runnable() {
@Override
public void run() {
try {
tvIntermediate.setText("");
tvExpert.setText("Relax...");
tvExpert.setTextSize(40);
tvExpert.setTextColor(Color.GREEN);
vibrate.vibrate(250);
clenchDone = false;
} catch (Exception e) {
e.printStackTrace();
}
if(i <= numTimesToRun) {
relaxHandler.postDelayed(relaxRunnable, time_out_variable);
i++;
}
}
};
然后对clenchHandler执行相同操作,使其运行x次
答案 1 :(得分:0)
在编码的心情,所以我要抓住它...
private static final int SWITCH_DELAY = 1000;
private Handler handler;
private boolean beginnerCycleActive = false;
private boolean clenched = false;
public final void onClick(View view) {
switch (view.getId()) {
case R.id.tvBeginner:
startBeginnerCycle();
break;
}
}
private void startBeginnerCycle(){
beginnerCycleActive = true;
tvBeginner.setText("Beginner exercise in progress...");
doCycle();
}
private void stopBeginnerCycle(){
beginnerCycleActive = false;
}
private void doCycle(){
onClench();
handler.postDelayed(new Runnable(){
public void run(){
onRelax();
handler.postDelayed(new Runnable(){
public void run(){
if(beginnerCycleActive) doCycle();
}
}, SWITCH_DELAY);
}
}, SWITCH_DELAY);
}
private void onClench(){
try {
tvExpert.setText("");
tvIntermediate.setText("Clench!!!");
tvIntermediate.setTextSize(60);
vibrate.vibrate(250);
clenched = true;
}
catch (Exception e) {
e.printStackTrace();
}
}
private void onRelax(){
try {
tvIntermediate.setText("");
tvExpert.setText("Relax...");
tvExpert.setTextSize(40);
tvExpert.setTextColor(Color.GREEN);
vibrate.vibrate(250);
clenched = false;
}
catch (Exception e) {
e.printStackTrace();
}
}
通过调用stopBeginnerCycle()
答案 2 :(得分:0)
你应该看看ScheduledThreadPoolExecutor。可以使用它在固定时间以looop周期运行Runnable
。下面的例子将向您展示它是如何工作的。
class YourRunnable implements Runnable {
@Override
public void run() {
//Do your stuff
}
}
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
long period = 10000; // the period between successive executions
exec.scheduleAtFixedRate(new YourRunnable (), 0, duration, TimeUnit.MICROSECONDS); // It can be seconds, miliseconds...
我认为在您的案例中使用ScheduledThreadPoolExecutor
非常容易。
答案 3 :(得分:0)
再次在runnable中调用postDelayed。