我正在开发一个应用程序,我想隐藏按钮,或者说,当我触摸屏幕时使按钮不可见,当我再次触摸屏幕时再次使按钮可见。
我怎么能做到?
实现一个 onTouchListener
// Catch touch events here
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
System.out.println("Touch Down X:" + event.getX() + " Y:" + event.getY());
}
if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("Touch Up X:" + event.getX() + " Y:" + event.getY());
}
return super.onTouchEvent(event);
}
然后在这个你会想要引用按钮并设置它的可见性:
button1.setVisibility(View.VISIBLE); or View.INVISIBLE
试试这个。它可能会解决您的问题。
LinearLayout mainLay = (LinearLayout) findViewById(R.id.lLay);
int flagForButton = 0;
Button b1 = new Button(this);
b1.setVisibility(0);
mainLay .setOnTouchListener(new OnTouch Listener)
{
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
if (action==0)
{
}
else
if (action == 1)
{
if (flagForButton ==1)
{
b1.setVisibility(0);
flagForButton = 0;
}
else
{
b1.setVisibility(4);
flagForButton = 1;
}
}
}
});