gpt4 book ai didi

java - 单击按钮时如何在执行之前取消计时器任务

转载 作者:行者123 更新时间:2023-12-01 11:42:04 26 4
gpt4 key购买 nike

我想知道如何在单击按钮时取消计时器任务..我正在做一个简单的数学游戏,因为我已经分配了一个计时器任务来更改 TextView 的文本,并且我想在单击按钮。但是当我这样做时我正在退出应用程序

public class MainActivity extends Activity implements OnClickListener {
Button b1, b2,b3;
ImageView i, i2, i3;
TextView t1, t2, t3,t4;
String s1, s2, s3, s4, s5;
LinearLayout pooku;
Button bk;
int q, m, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, n[];
Random r1 = new Random();
Random r0 = new Random();
Handler handler = new Handler();
Runnable r;
Timer t = new Timer();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);

pooku = (LinearLayout) findViewById(R.id.pooka);
i = (ImageView) findViewById(R.id.imageView1);
t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
t4= (TextView) findViewById(R.id.textView4);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);

imp();

}

public TimerTask malli(){
return new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {

public void run() {

t4.setText("times up");

t4.setVisibility(View.VISIBLE);

}

});
}
};
}

public void imp() {

t.schedule(malli(), 3000);



r2 = r1.nextInt(9) + 1;
r3 = r1.nextInt(9) + 1;
r4 = r2 + r3;
r5 = r2 - r3;
r6 = r2 * r3;

n = new int[] { r4, r5, r6 };

doit();
while (r9 == r11 || r9 == r13 || r11 == r13) {
doit();
}
s1 = Integer.toString(r2);
s2 = Integer.toString(r3);
s3 = Integer.toString(r9);
s4 = Integer.toString(r11);
s5 = Integer.toString(r13);
t1.setText(s1);
t2.setText(s2);
b1.setText(s3);
b2.setText(s4);
b3.setText(s5);

int[] images = { R.drawable.plus, R.drawable.minus, R.drawable.multiply };

Random random = new Random();




m = images[random.nextInt(images.length)];
i.setImageResource(m);
bk.setFocusable(true);
bk.requestFocus();
bk.requestFocusFromTouch();
}

public void doit() {
r9 = 0;
r11 = 0;
r13 = 0;
r8 = r1.nextInt(3) + 0;
r9 = n[r8];
r10 = r1.nextInt(3) + 0;
r11 = n[r10];
r12 = r1.nextInt(3) + 0;
r13 = n[r12];
int[] buttons = { R.id.button1, R.id.button2, R.id.button3 };
q = buttons[r0.nextInt(buttons.length)];
bk = (Button) findViewById(q);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
click(r9, b1);

break;
case R.id.button2:
click(r11, b2);
break;
case R.id.button3:
click(r13, b3);
break;
}
}

public void click(int k, Button b) {
if (m == R.drawable.plus && k == r4 || m == R.drawable.minus && k == r5
|| m == R.drawable.multiply && k == r6) {

t3.setVisibility(View.INVISIBLE);
t4.setVisibility(View.INVISIBLE);
this.t.cancel();
imp();
t4.setVisibility(View.INVISIBLE);

} else {
t3.setText("FINISH");
t3.setVisibility(View.VISIBLE);
b.setPressed(true);
b1.setClickable(false);
b2.setClickable(false);
b3.setClickable(false);
b.requestFocusFromTouch();

}
}
}

logcat 报告是

04-04 16:47:47.681: E/AndroidRuntime(11493): Process: com.example.emo2,  PID: 11493
04-04 16:47:47.681: E/AndroidRuntime(11493): java.lang.IllegalStateException: Timer was canceled
04-04 16:47:47.681: E/AndroidRuntime(11493): at java.util.Timer.scheduleImpl(Timer.java:561)
04-04 16:47:47.681: E/AndroidRuntime(11493): at java.util.Timer.schedule(Timer.java:459)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.example.emo2.MainActivity.imp(MainActivity.java:74)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.example.emo2.MainActivity.click(MainActivity.java:154)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.example.emo2.MainActivity.onClick(MainActivity.java:135)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.view.View.performClick(View.java:4443)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.view.View$PerformClick.run(View.java:18442)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.os.Handler.handleCallback(Handler.java:733)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.os.Handler.dispatchMessage(Handler.java:95)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.os.Looper.loop(Looper.java:136)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.app.ActivityThread.main(ActivityThread.java:5021)
04-04 16:47:47.681: E/AndroidRuntime(11493): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 16:47:47.681: E/AndroidRuntime(11493): at java.lang.reflect.Method.invoke(Method.java:515)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:82 7)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-04 16:47:47.681: E/AndroidRuntime(11493): at dalvik.system.NativeStart.main(Native Method)

最佳答案

放入按钮的 onClickListener():

如果(t!=空) t.cancel();

代码示例:

final TextView t1 = (TextView) findViewById(R.id.yourpay);

final Timer t =new Timer();
t.schedule(new TimerTask() {

@Override
public void run() {
runOnUiThread(new Runnable() {

public void run() {
money = (PPS+Reserve);
Reserve = (money);
t1.setText("$" + money); //Place your text data here

// Removed the stopping condition/counter

}
});
}
}, 1000, 1000); // Do you really want to wait 1 second before executing the timer's code? If not, change the 1st "1000" to a "0"


final Button b = (Button) findViewById(R.id.my_button_id); // Replace with your button's id
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (t != null)
t.cancel();
}
});

关于java - 单击按钮时如何在执行之前取消计时器任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29445714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com