android set visibility of a button on timer

泪湿孤枕 提交于 2019-12-20 10:35:03

问题


I have an app that shows a disclaimer at the beginning of the program. I want a button to remain invisible for a set amount of time, and then become visible. I set up a thread that sleeps for 5 seconds, and then tries to make the button visible. However ,I get this error when I execute my code:

08-02 21:34:07.868: ERROR/AndroidRuntime(1401): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

How can I count 5 seconds, and then make the button visible? THanks.

Thread splashTread = new Thread() {
           @Override
           public void run() {
            try {
                   int waited = 0;
                   while(_active && (!_ok2)) {
                       sleep(100);
                       if(_active) {
                           waited += 100;
                           if(waited >= _splashTime)
                           {
                            turnButtonOn();
                           }

                       }
                   }
               } catch(InterruptedException e) {
                   // do nothing
               } finally {
                   finish();
                   startActivity(new Intent("com.lba.mixer.Choose"));

               }
    };
    splashTread.start();


      public static void turnButtonOn() {
         okButton.setVisibility(View.VISIBLE);
      }

回答1:


The problem is that you're not in the UI thread when you call okButton.setVisibility(View.VISIBLE);, since you create and run your own thread. What you have to do is get your button's handler and set the visibility through the UI thread that you get via the handler.

So instead of

okButton.setVisibility(View.VISIBLE)

you should do

okButton.getHandler().post(new Runnable() {
    public void run() {
        okButton.setVisibility(View.VISIBLE);
    }
});



回答2:


I found this to be a much simpler solution. Visibility on 7 second delay

continuebutton.setVisibility(View.INVISIBLE);
continuebutton.postDelayed(new Runnable() {
        public void run() {
            continuebutton.setVisibility(View.VISIBLE);
        }
    }, 7000);



回答3:


I found this a Better solution to the problem (button id = but_resend)

define handler

  private Handler handler;

call function in extend class

 showButtons();

define after class

private void showButtons() {
        handler = new Handler();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ((Button) findViewById(R.id.but_resend)).setVisibility(View.VISIBLE);
            }
        }, 20000); // produce 20 sec delay in button visibility


    }

and keep in mind to hide the visibility in the.xml file by

android:visibility="invisible"


来源:https://stackoverflow.com/questions/3392147/android-set-visibility-of-a-button-on-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!