隐藏的布局后10秒在Android中? [英] Hide A Layout After 10 Seconds In Android?
问题描述
我有一个按钮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);
}
}
请使用 <$ C $的C>处理程序 和放大器; 的Runnable
。
您可以延迟一个Runnable使用<一个href="http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29"><$c$c>postDelayed$c$c>的处理程序。
的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);
更新(根据编辑的问题)
你只需要使用从处理程序
删除回调<一href="http://developer.android.com/reference/android/os/Handler.html#removeCallbacks%28java.lang.Runnable%29"><$c$c>removeCallbacks()$c$c>
所以只需更新 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);
这篇关于隐藏的布局后10秒在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!