0

我有一个按钮,按下后 3 秒就会消失。我想通过触摸屏幕使其再次可见。

public class MainActivity extends Activity {

    Runnable buttonRunnable;
    Handler buttonHandler = new Handler();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button=(Button)findViewById(R.id.myButton);
        buttonRunnable = new Runnable() {
            public void run() {
                button.setVisibility(View.GONE);

            }
        };

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {   

                buttonHandler.postDelayed(buttonRunnable, 3000);
            }
        });
    }



private OnTouchListener buttonOnTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final Button button=(Button)findViewById(R.id.myButton);
            final LinearLayout linearLayout=(LinearLayout)findViewById(R.id.myLinearLayout);

            linearLayout.setClickable(true);
            linearLayout.setOnClickListener(new OnClickListener() { 
                public void onClick(View view) {   

                    button.setVisibility(View.VISIBLE);
                }
            });
            return true;
        }

    };

}

我可以在 3 秒后隐藏按钮,但我无法让它再次可见。

4

2 回答 2

1

您需要获取对父容器视图的引用,并确保在其上调用 setClickable(true)。然后,将另一个单击处理程序附加到该视图,并在该单击处理程序中调用 button.setVisibility(View.VISIBLE);

于 2013-04-04T19:56:46.437 回答
1

我已经解决了这个问题:

public class MainActivity extends Activity{

    Runnable buttonRunnable;
    Handler buttonHandler = new Handler();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LinearLayout linearLayout=(LinearLayout)findViewById(R.id.myLinearLayout);
        final Button button=(Button)findViewById(R.id.myButton);
        buttonRunnable = new Runnable() {
            public void run() {
                button.setVisibility(View.INVISIBLE);

            }
        };

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {   

                buttonHandler.postDelayed(buttonRunnable, 3000);
            }
        });

        linearLayout.setOnTouchListener(buttonOnTouchListener);
    }

    private OnTouchListener buttonOnTouchListener = new OnTouchListener() { 
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final Button button=(Button)findViewById(R.id.myButton);
            button.setVisibility(View.VISIBLE);
            return true;
        }
    };
}
于 2013-04-05T14:22:55.593 回答