Androidのドキュメントを読んだところ、メソッド forceLayout() (次のレイアウト要求でレイアウト表示を生成するためのもの) と requestLayout() (即時のレイアウト要求を投稿することになっている) が見つかりましたが、それらを動作させることはできません宣伝どおり。特に、Thread.Sleep の前に 1 つのテキストを設定し、その後に 1 つのテキストを設定すると、両方のテキストを一度に設定する前に Sleep が終了するのを待ちます。UI スレッドで Thread.Sleep を呼び出してはならない方法について、多くのナンセンスで応答しないでください。Thread.Sleep を CountDownTimer でラップすると、完全にうまく機能します (スリープ時間を妨げないほど十分に短いティック時間と、スリープが終了するのに十分な長さのタイマーの期間がある限り。以下は次のとおりです。例:
int i=0;
TextView tv2;
TextView tv1;
LinearLayout ll;
Button bt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
tv1=new TextView(this);
tv2=new TextView(this);
bt=new Button(this);
bt.setText("Press to start");
ll.addView(bt);
ll.addView(tv1);
ll.addView(tv2);
tv2.setText("");
setContentView(ll);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tv1.setText("starting sleep");
new CountDownTimer(6000,50){
public void onTick(long msuf)
{if(i==1)
{
try{
Thread.sleep(4000);
tv2.setText("waking up");
}
catch(InterruptedException e){};
}
i++;
}
public void onFinish(){}}.start();
}
});
}