Java 如何在 5 秒延迟后自动单击 Android 中的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35708453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:02:14  来源:igfitidea点击:

How to automatically Click a Button in Android after a 5 second delay

javaandroidmobile

提问by Coder1

I have a small Android application that automatically clicks the button after 5 seconds. I have used performClick();but this does not work. When the timer gets to zero it simply crashes.

我有一个小的 Android 应用程序,它会在 5 秒后自动点击按钮。我用过,performClick();但这不起作用。当计时器归零时,它只会崩溃。

Here is my code:

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.local);
        getActionBar().setIcon(R.drawable.menu_drop);

        buttonClick();

        Thread timer = new Thread(){
            public void run(){ 
                try{
                    sleep(5000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }finally{
                    button1.performClick();
                }
            }
        };
        timer.start();
    } catch (NotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void buttonClick() {
    button1 = (Button) findViewById(R.id.button);
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(TestButton2.this, LocationView.class);
            startActivity(i);
        } 
    }); 
}

采纳答案by George Mulligan

You should post your logcat that includes the error message but one issue might be that you are accessing a UI element off the UI thread which isn't a good idea.

您应该发布包含错误消息的 logcat,但一个问题可能是您正在从 UI 线程访问 UI 元素,这不是一个好主意。

To do what you want you really don't need another thread. You can use a Handlerand a delayed Runnableinstead like below.

做你想做的事,你真的不需要另一个线程。您可以使用 aHandler和 a delayRunnable代替,如下所示。

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        button1.performClick();
    }
}, 5000);

This will schedule the Runnableto be executed on the UI thread after 5 seconds. If that still crashes post the stack trace from logcat.

这将安排Runnable在 5 秒后在 UI 线程上执行。如果仍然崩溃,请从 logcat 发布堆栈跟踪。

回答by TechnoBlahble

protected void onCreate(Bundle savedInstanceState) {
try {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.local);
    getActionBar().setIcon(R.drawable.menu_drop);

    //buttonClick();

    Thread timer = new Thread(){
        public void run(){ 
            try{
                sleep(5000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }finally{
                //button1.performClick();
                getLocationOnClick();
            }
        }
    };
    timer.start();
} catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

Change you button click method to like this -

将您的按钮单击方法更改为这样 -

public void getLocationOnClick(View v) {
     Intent i = new Intent(TestButton2.this, LocationView.class);
     startActivity(i);
    } 

In the xml for your Button add an onClick attribute -

在 Button 的 xml 中添加一个 onClick 属性 -

 <Button
        android:id="@+id/btnid"
        android:onClick="getLocationOnClick"
        .... >

回答by Amir Mohsenian

Use the runOnUiThread()method. This method run your method of UI thread

使用runOnUiThread()方法。此方法运行您的 UI 线程方法