使用Handler & Runnable 。
您可以使用postDelayed of Handler延迟Runnable。
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 } };
现在在onButtonClick事件中,您必须告诉Handler在X毫秒后运行runnable:
mHandler.postDelayed(mRunnable,10*1000);
如果要取消它,则必须使用mHandler.removeCallbacks(mRunnable);
更新(根据编辑的问题)您只需要使用removeCallbacks()从Handler删除回调
所以只需在onTouch方法中更新代码,如下所示:
mVolLayout.setVisibility(View.VISIBLE); mVolHandler.removeCallbacks(mVolRunnable); mVolHandler.postDelayed(mVolRunnable, 10000);
您可以使用单击按钮时启动的animation ,持续时间为10秒,淡出布局,并可能在最后将其可见性设置为GONE 。
以下代码会在3秒内隐藏您的布局。 您可以通过更改此处为延迟更改的常量来更改时间。
private void HideLayout() { final View view=volume_control_layout; view.postDelayed(new Runnable() { public void run() { if(!volume_controller.isPressed()) { view.setVisibility(View.INVISIBLE); } else { HideLayout(); } } }, 3000); // (3000 == 3secs) }
使用处理程序在10秒后隐藏布局。 使用Post延迟方法
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); pd=ProgressDialog.show(this,"","Loading, Please wait .. ",true); setTitle("set title"); final Handler uiThreadCallback=new Handler(); final Runnable runInUIThread= new Runnable(){ public void run(){ fnDraw(); pd.dismiss(); } }; new Thread(){ @Override public void run(){ uiThreadCallback.post(runInUIThread); } }.start(); } public void fnDraw(){ setContentView(R.layout.define ur xml if any); i1=(ImageView)findViewById(R.id.i1); t1=(TextView)findViewById(R.id.t1); t2=(TextView)findViewById(R.id.t2); timerRegister=new Timer(); lIteration=0; checkTime=new TimerTask(){ public void run(){ if(lIteration==1){ timerRegister.cancel(); uiDrawThreadCallback.post(runInUIDrawThread); } lIteration++; return; } }; timerRegister.scheduleAtFixedRate(checkTime,0,10000); } final Handler uiDrawThreadCallback=new Handler(); final Runnable runInUIDrawThread= new Runnable(){ @Override public void run(){ fnDrawAgain(); } }; public void fnDrawAgain(){ Intent intent=new Intent(this,new class you want to open.class); startActivity(intent); }
}
尝试它,确保它将在你的创建屏幕上工作
以上都不是好的解决方案。 想象一下,视图在10秒后不可见(例如:它向下滚动),因此Visibility.GONE将无法使用它。 我仍在寻找解决方案