I am working on an application where I want to hide 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?
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;
}
}
}
});