Hide A Layout After 10 Seconds In Android?

前端 未结 6 469
旧巷少年郎
旧巷少年郎 2021-02-04 07:30

I have a layout displayed on a button click.I want to hide that layout after 10 seconds.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-04 08:14

    Make use of Handler & Runnable.

    You can delay a Runnable using postDelayed of Handler.

    Runnable mRunnable;
    Handler mHandler=new Handler();
    
    mRunnable=new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    yourLayoutObject.setVisibility(View.INVISIBLE); //If you want just hide the View. But it will retain space occupied by the View.
                    yourLayoutObject.setVisibility(View.GONE); //This will remove the View. and free s the space occupied by the View    
                }
            };
    

    Now inside onButtonClick event you have to tell Handler to run a runnable after X milli seconds:

    mHandler.postDelayed(mRunnable,10*1000);
    

    If you want to cancel this then you have to use mHandler.removeCallbacks(mRunnable);

    Update (According to edited question) You just need to remove callbacks from Handler using removeCallbacks()

    So just update your code inside onTouch method like this :

    mVolLayout.setVisibility(View.VISIBLE);
    mVolHandler.removeCallbacks(mVolRunnable);
    mVolHandler.postDelayed(mVolRunnable, 10000);
    

提交回复
热议问题