2016-11-16 51 views
0

我正在開發一個android studio的程序。我希望當我點擊一個按鈕時,動作將同時執行。 功能: 月1日 - 開始計數點擊 2日 - 5秒後該按鈕禁用兩個onClick同時操作一個按鈕

setDelay = new Handler(); 

    btn = (Button) findViewById(R.id.bt); 

    final Button disableMe = (Button) findViewById(R.id.bt); 
    final TextView text = (TextView) findViewById(R.id.timeUp); 
    btn.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      startDelay = new Runnable(){ 
       @Override 
       public void run(){ 
        text.setText("Time Up!"); 
        disableMe.setEnabled(false); 
       } 
      }; 
      setDelay.postDelayed(startDelay, 5000); 

     } 
    }); 


    txv = (TextView) findViewById(R.id.tx); 

    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      mCounter ++; 
      txv.setText(Integer.toString(mCounter)); 

     } 


    }); 
+0

你想達到什麼是每次被點擊的按鈕,計數器遞增,並在第一次點擊5秒後被禁用? –

+0

我很感謝你的迴應,但我沒有得到它你能給我完整的代碼,我過去和它的工作。我想要一個按鈕,點擊它開始計數點擊,並在5秒後它可清除,並顯示秒數倒數 – user3476090

回答

0

這可以幫助

private int clicks = 0; 


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; 
     // You can update your text view here 
} 

顯示總點擊

private void showClicks(){ 
     Toast.makeText(this, Integer.toString(clicks), Toast.LENGTH_SHORT).show(); 
} 
+0

我很感謝你的迴應,但我沒有得到它可以給我完整的代碼,我過去和它的工作。我想要一個按鈕,點擊它開始計算點擊,5秒後它可見並且顯示秒數倒數 – user3476090

+0

查看此要點https://gist.github.com/Mtali/a1d42d19b4382130e81fbac8d9fcf45d –

+0

非常感謝您的先生。 你可以做一點幫助嗎?當我點擊按鈕時,它計數點擊,也顯示計數,並且還顯示5秒秒數計數禁用,如'countClicks()'中的1,2,3,4,5 – user3476090

0

創建新的列表如下:

ArrayList <View.OnClickListener> clickList = new ArrayList<>(); 

然後添加所有監聽器:

final Runnable startDelay; 
clickList.add(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      if (startDelay != null) 
       return; 
      startDelay = new Runnable(){ 
       @Override 
       public void run(){ 
        text.setText("Time Up!"); 
        disableMe.setEnabled(false); 
       } 
      }; 
      setDelay.postDelayed(startDelay, 5000); 
     } 
    }); 

clickList.add(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      mCounter ++; 
      txv.setText(Integer.toString(mCounter)); 

     } 
    }); 

然後,在您的按鈕中添加此監聽器:

btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      for (View.OnClickListener c : clickList) 
       c.onClick(view); 
     } 
    }); 
+0

這裏多次點擊會導致多個'Runnables'執行前禁用,這是錯誤的... –

+0

解決'if( startDelay!= null)return;',我不知道這個問題有什麼意義,但是我試着回答**兩個onClick動作一個按鈕在同一時間** –

0

只需使用一個boolean變量如果禁用Runnable開始講:

button.setOnClickListener(new View.OnClickListener() { 

      boolean disableRunnableStarted = false; 
      Runnable disableRunnable = new Runnable(){ 
       @Override 
       public void run(){ 
        //Running on UI thread to update button 
        MainActivity.this.runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          button.setEnabled(false); 
         } 
        }); 
       } 
      }; 
      @Override 
      public void onClick(View v) { 
       if(!disableRunnableStarted) { 
        Log.d(TAG,"Starting disable runnable and incrementing counter..."); 
        new Handler().postDelayed(disableRunnable,5000); 
        disableRunnableStarted = true; 

        //Here increment your counter 
       } else { 
        Log.d(TAG,"Just incrementing counter..."); 
        //Here increment your counter 
       } 
      } 
     }); 

替換MainActivityActivity的名字,如果在Fragment執行由getActivity()更換。