2017-05-31 80 views
0

我有一個按鈕,可以打開AlertDialog。然後,它開始倒計時,並刷新文本對話框內:如何在對話框中進行倒計時,並在對話框再次打開時恢復倒計時

private void countDown(int cooldown) { 
     countDownTimer = new CountDownTimer(cooldown, COOLDOWN_TIME_STEP_MILLIS) { //sec , steps 

      public void onTick(long millisUntilFinished) { 
       isTimerActive = false; 
       if (mFingerprintText != null) { 
        updateDialogText("Awsome text count: " + millisUntilFinished/COOLDOWN_TIME_STEP_MILLIS + " segs", false); 
       } 
      } 

      public void onFinish() { 
       isTimerActive = false; 
       if (mFingerprintText != null) { 
        updateDialogText("Awsome text count finished", false); 
       } 
      } 
     }.start(); 
    } 

但是,如果用戶關閉dialiog,並再次打開它,我要繼續在過去的時間倒計時。我的意思是,如果我在13:00:00開始倒計時(計數1分鐘),用戶在13:00:15關閉,如果用戶再次打開,例如:13:00:30,我想繼續計數,所以在這個時刻還需要再計數30秒......

我嘗試了一些不同的方法,但我沒有實現它。

private void showTimerDialog(Context context) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 

     View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_request_android_timer, null); 
     builder.setView(dialogView); 

     if (!isTimerActive) { 
      mTimerText = (TextView) dialogView.findViewById(R.id.dialog_timer_text); 
     } 

     builder.setOnDismissListener(new DialogInterface.OnDismissListener() { 
      @Override 
      public void onDismiss(DialogInterface dialog) { 
       if (mActive) { 
        deactivate(); 
       } 
      } 
     }); 

     builder.setOnCancelListener(new DialogInterface.OnCancelListener() { 
      @Override 
      public void onCancel(DialogInterface dialog) { 
       mDialog.dismiss(); 
      } 
     }); 

     builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       mDialog.dismiss(); 
      } 
     }); 

     mDialog = builder.create(); 
     mDialog.setCanceledOnTouchOutside(false); 
     mDialog.show(); 
    } 

回答

1

不要在本地創建對話框的對象。在Activity中創建一個全局變量。現在你的對話框創建和解僱可以成爲我們或方法showTimerDialog

覆蓋對話框的AlertDialog.BUTTON_POSITIVEAlertDialog.BUTTON_NEGATIVE .CALL類方法來處理這些場景

public class StartRaceFrag extends Activity{ 
//Keep a global instance 
private AlertDialog mDialog; 
//Create a boolean to check if timer already started or not 
private boolean isTimerstarted; 

現在的方法來創建對象,並推動對話

private void showTimerDialog(Context context) { 
     //Start timer based on this bool variable 
     if(!isTimerstarted){ 
     isTimerstarted=true; 
     } 
     final AlertDialog.Builder dialog = new AlertDialog.Builder(context); 
     View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_request_android_timer, null); 

     builder.setView(dialogView); 
     mDialog=builder.create(); 
     //Other dialog related statements 
     //Override the Dialog Positive and Negative button and call other class methods to handle 

現在基於這個bool變量,您可以修改運行計時器的方法

private void countDown(int cooldown) { 
    if(isTimerstarted){ 
    // Your business logic 
    }else{ 
    //Handle for else case 
    } 
} 
+0

我來看看,謝謝 – Shudy