Android Studio,延迟按钮

时间:2015-07-16 02:40:18

标签: android time delay android-button

int xp = 0;

public void practice(View v){

    xp = (xp+50);

    TextView tv = (TextView)findViewById(R.id.xp);
    tv.setText("XP: " + xp);
}

我是android的新手,我想要启用Button,当你点击它时会禁用它一段时间。我怎么能这样做?有什么例子吗?

2 个答案:

答案 0 :(得分:0)

试试这个

public void practice(View v){

    xp = (xp+50);

    yourBtn.setEnabled(false);
    handler.postDelayed(myRunnable, YOUR_DELAY);

    TextView tv = (TextView)findViewById(R.id.xp);
    tv.setText("XP: " + xp);
}

Handler代码是:

Handler  handler=new Handler();

Runnable myRunnable=new Runnable() {

        @Override
        public void run() {
            // call you delay logic or method here
           yourBtn.setEnabled(true);
        }
    }

答案 1 :(得分:0)

Thread btnEnabledThread = new Thread() {
            @Override
            public void run() {
                try {
yourBtn.setEnabled(false);
                    sleep(1000);

                } catch (InterruptedException i) {

                } finally {

yourBtn.setEnabled(true);
                }
            }
        };

如果要禁用1秒,请使用此代码

btnEnabledThread.start();