隐藏按钮并在触摸时再次显示

时间:2011-06-04 09:09:17

标签: android touch hide visible invisible

我正在开发一个应用程序,我想要隐藏按钮,或者说,当我触摸屏幕并再次按下<按钮 不可见 时当我再次触摸屏幕时,强> 可见

我怎么能成功?

2 个答案:

答案 0 :(得分:9)

实施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

答案 1 :(得分:0)

试试这个。它可能会解决您的问题。

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