问题描述:

I need may load at first bacground (setContentView(R.layout.activity_logo); ) after (6 second) Text in TextView4. but program make wrong. After start application, app wait 6 second and next build setContentView(R.layout.activity_logo); and write to TextView4

Why?

Thanks

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_logo);

try {

TimeUnit.SECONDS.sleep(6);

TextView textview4 = (TextView) findViewById(R.id.textView4);

textview4.setText("alalaalalalalalal");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

网友答案:
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logo);
            Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                   TextView textview4 = (TextView) findViewById(R.id.textView4);
        textview4.setText("alalaalalalalalal");
            }
        }, 6000);
}
网友答案:

sleep() blocks the UI thread and your UI cannot draw itself.

Use e.g. a Handler and postDelayed() to post a Runnable to run at a later time without blocking the UI thread.

相关阅读:
Top