开发者

Hide the button and show again on touch

I am working on an application where I want to h开发者_Go百科ide the button, or, say, make the button invisible when I touch the screen and again make the button visible when I again touch the screen.

How I can make it?


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

Then in this you'll want to reference the button and set it's visibility:

  button1.setVisibility(View.VISIBLE); or View.INVISIBLE


Try this. It might solve your problem.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜