Android 如何仅显示按钮5秒钟

Android 如何仅显示按钮5秒钟,android,timer,Android,Timer,我想知道如何显示一个按钮只有五秒钟,如果点击,那么定时器应该增加另外五秒钟,按钮在不同的位置。我使用scheduleatfixedrate方法,但一旦单击它,它就会停止我的应用程序。我的平台是android。请帮帮我 试试这个,这里没有介绍定位,因为我不知道你想要什么,但是可见性已经改变了 private long lastClicked; @Override protected void onCreate(Bundle savedInstanceState) { super.onCr

我想知道如何显示一个按钮只有五秒钟,如果点击,那么定时器应该增加另外五秒钟,按钮在不同的位置。我使用scheduleatfixedrate方法,但一旦单击它,它就会停止我的应用程序。我的平台是android。请帮帮我

试试这个,这里没有介绍定位,因为我不知道你想要什么,但是可见性已经改变了

private long lastClicked;

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

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lastClicked = System.currentTimeMillis();
        }
    });
    lastClicked = System.currentTimeMillis();
    button.postDelayed(new Runnable() {
        @Override
        public void run() {
            if(System.currentTimeMillis() - lastClicked > 5000) {
                button.setVisibility(View.INVISIBLE);
            }
            else {
                button.postDelayed(this, 1000);
            }
        }
    }, 1000);
}