2014-09-03 54 views
2

在我的應用程序中,我將推送通知顯示爲具有兩個按鈕的對話框是。我需要在對話框標題中顯示一個計時器(20秒)。安裝了CountDown TImer的對話框

如果用戶點擊是的,它應該去一個活動。
如果用戶單擊,在定時器結束之前對話框會被取消。
倒計時結束後,對話框應該消失。我應該如何實現這一點?

這裏是我的警報對話框方法

public void showAlertDialog(final Context context, String title, String message, 
          Boolean status) { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 

    // Setting Dialog Title 
    alertDialog.setTitle(title); 

    // Setting Dialog Message 
    alertDialog.setMessage(message); 

    // Setting Icon to Dialog 
    //alertDialog.setIcon(R.drawable.fail); 

    // Setting Positive "Yes" Button 
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); 

     } 
    }); 

    // Setting Negative "NO" Button 
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show(); 

      dialog.cancel(); 
     } 
    }); 

    // Showing Alert Message 
    //alertDialog.setIcon(R.drawable.counter); 
    alertDialog.show(); 
} 
+0

具有u您的應用 – 2014-09-03 09:20:18

+0

歌廳PUCH notifecation是我得到 – user1799171 2014-09-03 09:21:09

+0

創建接收器檢查PUCH通知來在的onReceive基法打開的對話框時PUCH notificatin加油設備 – 2014-09-03 09:22:34

回答

1

我不建議加入到倒計時對話框的標題。如果您將其添加到按鈕,則更明顯的是用戶在倒計時完成後會發生什麼。

下面是AlertDialog的代碼,可以做到這一點。

AlertDialog dialog = new AlertDialog.Builder(this) 
     .setTitle("Notification Title") 
     .setMessage("Do you really want to delete the file?") 
     .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO: Add positive button action code here 
      } 
     }) 
     .setNegativeButton(android.R.string.no, null) 
     .create(); 
dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
    private static final int AUTO_DISMISS_MILLIS = 6000; 
    @Override 
    public void onShow(final DialogInterface dialog) { 
     final Button defaultButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE); 
     final CharSequence positiveButtonText = defaultButton.getText(); 
     new CountDownTimer(AUTO_DISMISS_MILLIS, 100) { 
      @Override 
      public void onTick(long millisUntilFinished) { 
       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()) { 
        dialog.dismiss(); 
       } 
      } 
     }.start(); 
    } 
}); 
dialog.show(); 

dialog with countdown screenshot

+0

相當不錯的解決方案;另外 - 我相信,你應該記住timer var並在對話框中取消它。 – 2017-07-05 18:05:26