2016-11-17 70 views
0

我開發一個程序,當我點擊一個按鈕開始計算點擊次數並顯示計數時,並且在5秒後該按鈕是可以理解的,並且我也想顯示倒計時秒數不顯示同時在一個按鈕上執行三個onClick操作

mTextView = (TextView) findViewById(R.id.total_textview); 
    mTextView.setVisibility(View.VISIBLE); 

    Button button = (Button) findViewById(R.id.count_button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(final View v) { 
      final Button b = (Button)v; 
      if (clicks == 0){ 
       // Means its the first time that a user click the button 
       // Start a thread that is going to disable the button after 5 seconds from first click 
       new Thread(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          Thread.sleep(5000); 
          runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            b.setText("Time up"); 
            b.setEnabled(false); 
            // Showing user clicks after button is disabled 
            showClicks(); 
           } 
          }); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
       }).start(); 
      } 
      // Here we are just counting . . . . including the first click 
      countClicks(); 
     } 
    }); 
} 


private void countClicks(){ 
    ++clicks; 
    mTextView.setText(Integer.toString(clicks)); 

    // You can update your text view here 
} 
private void showClicks(){ 
    mTextView.setText(String.valueOf(clicks)+"Clicks"); 
    mTextView.setVisibility(View.VISIBLE); 

} 

}

回答

1

我建議你看一看CountDownTimer

這裏更新UI的修改代碼。它會持續1秒鐘,並將TextView更新5秒鐘。最後,它說: 「時間上」:

new CountDownTimer(5000, 1000) { 

    public void onTick(long millisUntilFinished) { 
     b.setText("Seconds remaining: " + millisUntilFinished/1000); 
    } 

    public void onFinish() { 
     b.setText("Time up"); 
     b.setEnabled(false); 
     // Showing user clicks after button is disabled 
     showClicks(); 
    } 
    }.start(); 
0

請使用一個有些變化,現有的代碼:

mTextView = (TextView)view.findViewById(R.id.total_textview); 
     mTextView.setVisibility(View.VISIBLE); 

     button = (Button) view.findViewById(R.id.count_button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(final View v) { 
       final Button b = (Button) v; 
       if (clicks == 0) { 
        setCountDown(); 
        // Means its the first time that a user click the button 
        // Start a thread that is going to disable the button after 5 seconds from first click 
        new Thread(new Runnable() { 
         @Override 
         public void run() { 
          try { 
           Thread.sleep(5000); 
           getActivity().runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             b.setText("Time up"); 
             b.setEnabled(false); 
             // Showing user clicks after button is disabled 
             showClicks(); 
            } 
           }); 
          } catch (InterruptedException e) { 
           e.printStackTrace(); 
          } 
         } 
        }).start(); 
       } 
       // Here we are just counting . . . . including the first click 
       countClicks(); 
      } 
     }); 





private void countClicks(){ 
     ++clicks; 
     mTextView.setText(Integer.toString(clicks)); 

     // You can update your text view here 
    } 
    private void showClicks(){ 
     mTextView.setText(String.valueOf(clicks)+"Clicks"); 
     mTextView.setVisibility(View.VISIBLE); 

    } 


    private void setCountDown(){ 
     t = new Timer(); 
     t.scheduleAtFixedRate(new TimerTask() { 

            @Override 
            public void run() { 
             handler = new Handler(Looper.getMainLooper()); 
             handler.post(new Runnable() { 
              public void run() { 
               if (i > 0) { 
                button.setText("Time Left :" + Integer.toString(i--)); 
               } else { 
                t.cancel(); 
               } 
              } 
             }); 
            } 

           }, 
//Set how long before to start calling the TimerTask (in milliseconds) 
       0, 
//Set the amount of time between each execution (in milliseconds) 
       1000); 
    } 

INT I = 5實例級別

定時器噸; // Java的util的計時器

Hanler處理; // Android操作系統處理程序

相關問題