2015-04-04 70 views
0

我想知道當按鈕被點擊時如何取消定時器任務..我正在做一個簡單的數學遊戲,因爲我已經分配了一個時間任務改變一個TextView的文本,我想取消按鈕時clicked.But我得到了應用程序的時候我這樣做如何在點擊按鈕之前取消定時器任務

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) 
+0

你是什麼意思 「出來的應用程序」? – 2015-04-04 11:19:03

+0

它崩潰說「不幸你的應用程序已停止工作」 – 2015-04-04 11:35:17

回答

1

把你的按鈕的onClickListener():

如果(T!= NULL) 噸。取消();

代碼示例:

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(); 
     } 
}); 
+0

但是,我還有一個問題,如果我在3秒鐘之前按下按鈕,任務必須取消,但在這裏即使當我在3秒內按下按鈕不止一次,文本在第一個3秒後發生變化點擊然後在3秒鐘之前到達第二次點擊.... @sohail – 2015-04-04 12:10:57

0

只需撥打對Timer對象在您的屁股的第一個鏈接在onClick方法。

+0

我做了這件事...檢查點擊(); – 2015-04-04 11:26:10

+0

嘗試在任何邏輯檢查之前將它放在'onClick'的第一行 – 2015-04-04 11:30:28

+0

您必須每次創建一個新的'Timer'對象,否則不能使用同一個對象。 – 2015-04-04 11:34:50

0

您可以使用此標誌。 在onclick中,您可以將一個標誌設置爲false,然後在您的計時器中執行一次檢查,以便只在該標誌爲true/set時執行。 而不是this.t.cancel在你的OnClick寫flag = false

,然後在你的計時器任務

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

       public void run() { 

        if(flag){ 
         t4.setText("times up"); 
         t4.setVisibility(View.VISIBLE); 
        } 
       } 

      }); 
    } 
}; 
相關問題