隐藏的布局后10秒在Android中?布局、Android

2023-09-05 03:25:28 作者:10.背叛丶世界

我有一个按钮click.I显示的布局要在10秒钟后隐藏的布局。

 保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

mVolHandler =新的处理程序();
        mVolRunnable =新的Runnable(){
            公共无效的run(){
                mVolLayout.setVisibility(View.GONE);

            }
        };
}


私人OnTouchListener mVolPlusOnTouchListener =新OnTouchListener(){

        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            mVolLayout.setVisibility(View.VISIBLE);

            mVolHandler.postDelayed(mVolRunnable,10000);
}
}
 

解决方案

请使用 处理程序 和放大器; 的Runnable

Android 布局

您可以延迟一个Runnable使用postDelayed的处理程序。

 的Runnable mRunnable;
处理器mHandler =新的处理程序();

mRunnable =新的Runnable(){

            @覆盖
            公共无效的run(){
                // TODO自动生成方法存根
                yourLayoutObject.setVisibility(View.INVISIBLE); //如果你只想隐藏查看。但是,将保留通过查看所占用的空间。
                yourLayoutObject.setVisibility(View.GONE); //这将删除视图。并占领了查看自由S中的空间
            }
        };
 

现在onButtonClick事件中,你必须告诉处理器于X毫秒后运行一个可运行的:

  mHandler.postDelayed(mRunnable,10 * 1000);
 

如果您想取消,那么你必须使用 mHandler.removeCallbacks(mRunnable);

更新(根据编辑的问题) 你只需要使用从处理程序删除回调removeCallbacks()

所以只需更新 onTouch 这样的方法在你的code:

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

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(savedInstanceState);

mVolHandler = new Handler();
        mVolRunnable = new Runnable() {
            public void run() {
                mVolLayout.setVisibility(View.GONE);

            }
        };
}


private OnTouchListener mVolPlusOnTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mVolLayout.setVisibility(View.VISIBLE);

            mVolHandler.postDelayed(mVolRunnable, 10000);
}
}

解决方案

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