简体   繁体   中英

layout is not shown after waiting

I have three layouts:

Layout1
-->onClick()-->show
Layout2
-->wait three seconds-->show
Layout3

The problem is that Layout2 is not shown. To set the layouts I use

setContentView(int);

The relevant code might be:

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);
    }
}

My idea was that Android might use something like an "Event-Loop" (like Qt) and my method would block the control to get back to the "Event-Loop" which would make the layout displayed. But I couldn't find my error.

The problem why your layout2 is not shown is because of TimeUnit.MILLISECONDS.sleep(3000); - what you are doing here is you put your UI thread into sleep, so UI thread cannot process your request to change layout. And when it wakes up - it immediately sets layout3 that's why layout2 is not shown.

You might consider using Handler.postDelayed(Runnable, long) to postpone execution

So this should work as you expected:

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);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                setContentView(R.layout.layout3);
            }
        }, 3000);

    }
}

Try this, it will surely work

    public void changeLayouts() {
         setContentView(R.layout.layout2);

        Thread Timer = new Thread(){
       public void run(){
          try{
              sleep(3000);
          } catch(InterruptedException e){
              e.printStackTrace();
          } finally {
               setContentView(R.layout.layout3);                
          }
       }    
    }; Timer.start();
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM