2014-10-04 31 views
-1

,我剛剛開始爲android學習java,我已將TextVisibility的textfield設置爲GONE,我需要TextVisibilty在從Button點擊7秒後變爲可見,我搜索了互聯網,找不到有用的東西或我無法理解。所以,請幫助我。經過一段時間之後的TextVisibility更改

回答

0

在Android上,當你想後的一段時間內,在其他線程中執行的東西,你通常使用處理器像這樣:

例如,在OnCreate中(你的活動)
final TextView yourText = (TextView) findViewById(R.id.yourTextId); 
Handler handler = new Handler(); 
handler.postDelayed(new Runnable()) { 
@Override 
    public void run() { 
     yourText.setVisibility(View.VISIBLE); 
    } 
}, 7000); 

如果你願意,你可以實現Runnable類的活動,所以您不必封裝什麼:

public class MyActivity implements Runnable 

然後你通過這個脫postDelayed方法的第一個參數,

handler.postDelayed(this, 7000); 

並實現您的活動中的run方法:

public void onCreate() { 
    super.onCreate(); 
    setContentView(R.id.your_layout); 
    /* some code */ 
} 

@Override 
public void run() { 
    yourText.setVisibility(View.VISIBLE); 
} 

這之後的執行run方法中的代碼您在postDelayed的第二個參數中設置了一段時間,並且在另一個線程內部,因此您可以繼續在MainActivity中創建內容,例如單擊按鈕或其他內容。

編輯與徵求意見的示例代碼:

Handler handler1 = new Handler(); 
Handler handler2 = new Handler(); 
handler1.postDelayed(new Runnable() { 
    tvt1.setVisibility(View.VISIBLE); 
}, 7000); 
handler2.postDelayed(new Runnable() { 
    tvt2.setVisibility(View.VISIBLE); 
}, 12000); 
+0

非常感謝您的幫助! :D – Kdvinmk 2014-10-04 10:40:04

+0

另一位助手,我可以在一段時間後播放音軌,如果是的話,你能幫我嗎? – Kdvinmk 2014-10-04 11:19:37

+0

@Kdvinmk在一段時間後玩的邏輯是一樣的,只是把你想要做的內部運行方法。我沒有關於如何處理音頻的知識,因爲我對Android很陌生,但是您應該從開發人員網站獲得它,還有一部分[管理音頻播放]的培訓(http://developer.android .COM /培訓/管理音頻/ index.html的)。 – tibuurcio 2014-10-04 11:24:47

0

嘗試

final TextView yourText = (TextView) findViewById(R.id.yourTextId); 
yourText.setVisibility(View.GONE); 
yourText.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      yourText.setVisibility(View.VISIBLE); 
     } 
    }, 7 * 1000); 

在你點擊按鈕監聽器。

final TextView yourText = (TextView) findViewById(R.id.yourTextId); 
Button yourButton = (Button) findViewById(R.id.yourButtonId); 
yourButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       yourText.setVisibility(View.GONE); 
       yourText.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         yourText.setVisibility(View.VISIBLE); 
        } 
       }, 7 * 1000); 
       } 
     }); 
+0

另一個幫助夥計,我可以打一個音軌一段時間後,如果是的話,你能幫幫我嗎? – Kdvinmk 2014-10-04 11:15:48

相關問題