我在点击按钮上显示了一个布局。我希望在10秒后隐藏该布局。
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);
}
}
答案 0 :(得分:36)
您可以使用Handler的postDelayed
来延迟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);
答案 1 :(得分:2)
单击按钮时,您可以使用Animation启动,持续时间为10秒,淡出布局,并可能在最后将其可见性设置为GONE
。
答案 2 :(得分:1)
以下代码会在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)
}
答案 3 :(得分:0)
使用处理程序在10秒后隐藏布局。使用后延迟方法
答案 4 :(得分:0)
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);
}
}
尝试一下,我确信它会在你的创建屏幕上运行
答案 5 :(得分:0)
以上都不是好的解决方案。想象一下,10s后视图不可见(例如:它向下滚动),因此Visibility.GONE将无法使用它。 我仍在寻找解决方案