後不顯示我有三個佈局:佈局等待
Layout1
-->onClick()-->show
Layout2
-->wait three seconds-->show
Layout3
的問題是,佈局2不顯示。要設置我用
setContentView(int);
佈局相關的代碼可能是:
public class TrainingActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
final Button inputButton = (Button)findViewById(R.id.inputButton);
inputButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
changeLayouts();
}
});
}
public void changeLayouts() {
setContentView(R.layout.layout2);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.layout3);
}
}
我的想法是,Android人物可能會使用類似的「事件循環」(如QT)和我的方法將阻塞控制返回到「事件循環」,這將使佈局顯示。 但我找不到我的錯誤。
重新考慮你的設計。爲什麼在第一個地方爲同一個活動多次使用setContentView? – Raghunandan
我不知道爲什麼這個具體失敗,我很驚訝它沒有引發NotResponding。如果您希望在暫停三秒後觸發UI操作,請考慮使用[AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html)。 – DevOfZot