2012-05-28 159 views
3

怎樣在我的警告框顯示倒計時。我要通知,本次會議將在5分鐘內結束,並顯示在警報彈出框的Android倒計時

運行的定時器,對用戶的警告框
+0

您已經嘗試了什麼?代碼 – Akram

回答

2

創建上有一個TextView自定義對話框。

和更新CountDownTimer類這樣的幫助,代碼。

new CountDownTimer(300000, 1000) { 

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

    public void onFinish() { 
     mTextField.setText("done!"); 
    } 
    }.start(); 

您可以關閉對話框onFinish()

更多細節可以follow this link

+0

我想表明的警告框 – saravanan

+0

剩餘秒數@saravanan耶在object.setMessage是 – Akram

+0

它的一個警告框對象(「秒時:」 + millisUntilFinished/1000)使用自定義對話框;它的正確與否,請告訴我 – saravanan

2

下面的代碼作爲描述你創建一個提示。它向倒數計時器添加一個默認動作按鈕。

screenshot of dialog with countdown 對話框監聽器類

private static class DialogTimeoutListener 
     implements DialogInterface.OnShowListener, DialogInterface.OnDismissListener { 
    private static final int AUTO_DISMISS_MILLIS = 5 * 60 * 1000; 
    private CountDownTimer mCountDownTimer; 

    @Override 
    public void onShow(final DialogInterface dialog) { 
     final Button defaultButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE); 
     final CharSequence positiveButtonText = defaultButton.getText(); 
     mCountDownTimer = new CountDownTimer(AUTO_DISMISS_MILLIS, 100) { 
      @Override 
      public void onTick(long millisUntilFinished) { 
       if (millisUntilFinished > 60000) { 
        defaultButton.setText(String.format(
          Locale.getDefault(), "%s (%d:%02d)", 
          positiveButtonText, 
          TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), 
          TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished % 60000) 
        )); 
       } else { 
        defaultButton.setText(String.format(
          Locale.getDefault(), "%s (%d)", 
          positiveButtonText, 
          TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) + 1 //add one so it never displays zero 
        )); 
       } 
      } 

      @Override 
      public void onFinish() { 
       if (((AlertDialog) dialog).isShowing()) { 
        // TODO: call your logout method 
        dialog.dismiss(); 
       } 
      } 
     }; 
     mCountDownTimer.start(); 
    } 

    @Override 
    public void onDismiss(DialogInterface dialog) { 
     mCountDownTimer.cancel(); 
    } 

警告對話框

AlertDialog dialog = new AlertDialog.Builder(this) 
     .setTitle("Session Timeout") 
     .setMessage("Due to inactivity, you will soon be logged out.") 
     .setPositiveButton("Extend Session", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO: call your log out method 
      } 
     }) 
     .setNegativeButton("Log Out Now", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO: call method to extend session 
      } 
     }) 
     .create(); 
DialogTimeoutListener listener = new DialogTimeoutListener(); 
dialog.setOnShowListener(listener); 
dialog.setOnDismissListener(listener); 
dialog.show();