2012-02-22 145 views
2

我有需要,我不得不自動關閉警報對話框中2種不同的情況下自動關閉警報對話框

  1. 自動關閉對話框後,我得到它我在等待
  2. 自動返回值 - 在用戶沒有輸入10秒後關閉對話框。我知道我應該使用某種計時器,但不知道如何將它與對話框連接起來。

我知道並理解它不正確的方式來處理用戶界面,但我的要求需要我這樣做。

請分享你的想法,

感謝,

SKU

回答

0

從AlertDialog,在那裏你處理你的計時器onStart繼承自定義對話框。使用AsyncTask將是很好的倒計時。

自動關閉對話框不是什麼壞事...至少我們總是看到這個在改變屏幕分辨率(贏得XP),這是很好的這種情況。也許你還可以在按鈕上加入倒數計時器(如「5秒內關閉」)。

4

1-對於第一種情況:

AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create(); 
... 
alertDialog.show(); 
int valueIamWaitingFor = 5; 
if (aValue == valueIamWaitingFor){ 
    alertDialog.hide(); 
} 

2-對於第二種情況:

private static final ScheduledExecutorService executor = 
    Executors.newSingleThreadScheduledExecutor(); 
public AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create(); 
... 
alertDialog.show(); 
Runnable hideDialog= new Runnable() { 
    public void run() { 
     this.alertDialog.hide(); 
    } 
}; 
executor.schedule(hideDialog, 10, TimeUnit.SECONDS); 
+0

嗨扎卡里亞......我跟着你與案例2做了什麼。但WRT的情況1,我正在等待返回值的字符串,這將大多數最少4秒(從一個單獨的線程)到最大8秒返回。我不確定您的案例1的代碼片段是否可以使用?你能解釋一下那部分嗎? – sku 2012-02-22 03:19:22

+0

@sku:出於好奇,是什麼讓你確定該值會在8秒內返回最大值? – Zakaria 2012-02-22 08:02:07

+0

它只是因爲長時間的觀察,如果它沒有在8秒內回答答案......有什麼問題。 – sku 2012-02-22 21:36:56