[英]How to add “Yes” and “No” options to auto-close AlertDialog?
我猜是第一次。 這是我的第一個問題。
我正在開發汽車應用程序,在這里我需要在駕駛時盡量減少用戶交互。 該應用程序需要不時向用戶建議其做出的“決定”。 我們稱之為“綠色”。 然后,應用程序顯示AlertDialog
,告知用戶該決定,並讓用戶:
我正在嘗試使用AlertDialog
和Timer
來實現:
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.