簡體   English   中英

如何添加“是”和“否”選項以自動關閉AlertDialog?

[英]How to add “Yes” and “No” options to auto-close AlertDialog?

我猜是第一次。 這是我的第一個問題。

我正在開發汽車應用程序,在這里我需要在駕駛時盡量減少用戶交互。 該應用程序需要不時向用戶建議其做出的“決定”。 我們稱之為“綠色”。 然后,應用程序顯示AlertDialog ,告知用戶該決定,並讓用戶:

  • 等待3秒鍾,然后對話框消失,並強制執行“ GREEN”
  • 按對話框上的“紅色”按鈕,此決定改為“紅色”

我正在嘗試使用AlertDialogTimer來實現:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Seems it was GREEN");
builder.setMessage("You can change it to RED by pressing a button");
builder.setCancelable(true);
builder.setPositiveButton("RED!", new DialogInterface.OnCLickListener() {
    public void onClick(DialogInterface dialog, int whichButton) [
        // 1. Do things required by RED option
    }
} 

final AlertDialog dlg = builder.create();

dlg.show();

final Timer t = new Timer();
t.schedule(new TimerTask() {
    public void run() {
        // 2. Do things required by GREEN option
        dlg.dismiss(); 
        t.cancel();
    }
}, 3000);

問題是,在始終執行1.,2之后,因此,如果用戶嘗試通過按“ RED”按鈕“取消”“ GREEN”,則“ GREEN”選項仍在“ RED”之后運行。

我該如何排除其他因素,以便當應用通過對話框通知某項決定時,等待幾秒鍾,如果用戶未選擇其他決定,則執行該決定?

取消您的計時器-

builder.setPositiveButton("RED!", new DialogInterface.OnCLickListener() 
{
    public void onClick(DialogInterface dialog, int whichButton) 
    {
        if(t != null)
        {
            t.cancel();
        }
    }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM