2012-07-01 69 views
4

我想爲我的程序創建一些簡單的動畫。 有4個不可見的按鈕,我想要的是程序啓動時,這些按鈕將延遲顯示。延遲顯示按鈕Android Java

顯示按鈕1 - >按鈕2 - >依此類推。

我試過這個,但是當程序運行時,所有按鈕都會在同一時間出現。

try { 
((Button) findViewById(R.id.f1)).setVisibility(View.VISIBLE); 
    Thread.sleep(1200); 
((Button) findViewById(R.id.f2)).setVisibility(View.VISIBLE); 
    Thread.sleep(1200); 
((Button) findViewById(R.id.f3)).setVisibility(View.VISIBLE); 
    Thread.sleep(1200); 
((Button) findViewById(R.id.f4)).setVisibility(View.VISIBLE); 
    Thread.sleep(1200); 
} catch (Exception e) { 
} 

任何人都可以幫助我嗎?

回答

6

使用處理:

private Handler handler; 

private void showButtons(){ 
    handler = new Handler(); 

handler.postDelayed(new Runnable(){ 
    @Override 
    public void run(){ 
     ((Button) findViewById(R.id.f1)).setVisibility(View.VISIBLE); 
    } 
}, 1200); 

handler.postDelayed(new Runnable(){ 
    @Override 
    public void run(){ 
     ((Button) findViewById(R.id.f2)).setVisibility(View.VISIBLE); 
    } 
}, 2400); 

//etc 
} 
+0

它工作夥計!謝謝 – felangga

0

這不符合這個問題涉及,但如果你的睡眠主線程(就像你做了什麼) 它可以使你的應用程序凍結

+0

Handler.PostDelayed不睡覺主線程 –

-2
use handler for your purpose 
handler is thread which only can be used for updating UI apart from UI thread. 

private Handler handler; 

private void showButtons(){ 
    handler = new Handler(); 

handler.postDelayed(new Runnable(){ 
    @Override 
    public void run(){ 
     ((Button) findViewById(R.id.f1)).setVisibility(View.VISIBLE); 
    } 
}, 1200); 

handler.postDelayed(new Runnable(){ 
    @Override 
    public void run(){ 
     ((Button) findViewById(R.id.f2)).setVisibility(View.VISIBLE); 
    } 
}, 2400); 

//etc 
} 


or u can use handler.sendEmptyMessage(int what); 
their u make a logic of vissibility and invissible 
+1

你剛剛複製我的答案? – nhaarman

+0

改進了您的答案Niek的版權 – Vinayak