2011-11-18 85 views
13

我有一个按钮click.I显示的布局要在10秒后以隐藏布局。在Android 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

我试过使用handler.when我点击按钮布局显示和使用处理程序它是隐藏后10秒。但问题是,当不断点击按钮它隐藏。如果按钮再次点击,布局显示,但在10秒前隐藏。我想要显示布局10秒,从最后一次用户点击。 – user987362

+0

我有没有其他的解决办法吗?请帮我 – user987362

+0

我已经试过这样的 – user987362

回答

34

利用Handler & Runnable肯定它会工作。

可以使用处理器的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事件中你必须告诉处理器X毫秒秒后运行一个可运行:

mHandler.postDelayed(mRunnable,10*1000); 

如果要取消这个,那么你必须使用mHandler.removeCallbacks(mRunnable);

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

所以只要内部onTouch方法更新你这样的代码:

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

您可以使用Animation开始,当你按一下按钮,10秒时间淡出的布局和大概将在结束其知名度GONE

+0

+1提供一个好的解决方案尽管使用计时器也能达到目的..如果你已经展示了它的实际完成情况,它将会很有帮助。 – ngesh

+1

让布局消失不过是不高雅的,这就是为什么我建议动画。 –

0

使用处理器到10秒后隐藏布局。使用后延迟方法

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

}

试试吧M在乌拉圭回合上创建屏幕

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) 

    } 
0

以上都不是好的解决方案。想象一下,10秒后视图是不可见的(例如:向下滚动),因此Visibility.GONE无法工作。 我仍在寻找解决方案