java 打开应用程序时如何自动调用按钮的onClick?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7750262/
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-10-30 21:15:41  来源:igfitidea点击:

How to automatically call onClick for a Button when application is opened?

javaandroid

提问by user971035

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            loadFeed(statutory);
        }
    });
}

I want my app to automatically call onClickfor my Button b1when I open my application.

我想我的应用程序自动调用onClick我的Button b1,当我打开我的应用程序。

How can I make my app automatically call onClickwhen the app is opened? I am looking for a modification to my existing code to accomplish this.

如何让我的应用onClick程序在打开时自动调用?我正在寻找对我现有代码的修改来实现这一点。

回答by Android Killer

If you want something automatic then put that code inside a function and call it from inside the onCreate()function.

如果你想要一些自动的东西,那么把代码放在一个函数中并从onCreate()函数内部调用它。

For example:

例如:

public class YourActivity extends Activity {

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        function();
    }

    public void function() {
        // Your code here
    }

}

回答by Vladimir

If I got you right, and you need to genereate click event on activity start, add

如果我猜对了,并且您需要在活动开始时生成点击事件,请添加

b1.performClick();

after setting listener. Yet I don't understand why can't you just call loadFeed(statutory);directly

设置监听器后。然而我不明白你为什么不能loadFeed(statutory);直接打电话

回答by Paresh Mayani

Instead why don't you call your loadFeed(statutory) directly in onCreate() method, if you do this then it will get automatically called when your application gets start.

相反,为什么不直接在 onCreate() 方法中调用您的 loadFeed(statutory),如果您这样做,那么它会在您的应用程序启动时自动调用。

Update:

更新:

Actually, i don't know about performClick()method existed there, so just use button1.performClick() after assign click listener for your button. Thanx sagar for the pointing it out.

实际上,我不知道那里存在performClick()方法,因此只需在为按钮分配单击侦听器后使用 button1.performClick() 即可。感谢 sagar 指出它。

回答by MKJParekh

Reading your question i understood that you have written a button click listener and the code you have written in that..you want to execute it automatically when the Activity starts

阅读您的问题后,我了解到您编写了一个按钮单击侦听器以及您在其中编写的代码。您希望在 Activity 启动时自动执行它

As you have implemented a Listener that will get executed only when the appropriate event occurs for the Activity Start there are events that you can use are like onCreate() and onReStart()

由于您已经实现了一个仅在 Activity Start 发生适当事件时才会执行的侦听器,因此您可以使用一些事件,例如 onCreate() 和 onReStart()

But you can't call any event listener without happening of the event.

但是你不能在没有发生事件的情况下调用任何事件侦听器。

doucmentations about the events and listeners those are used listed below and you can also make custom listener but cant call any listener like a function

下面列出了有关事件和侦听器的文档,您还可以制作自定义侦听器,但不能像函数一样调用任何侦听器

http://developer.android.com/guide/topics/ui/ui-events.html

http://developer.android.com/guide/topics/ui/ui-events.html

http://developer.motorola.com/docstools/library/Basics_of_Event_Management/

http://developer.motorola.com/docstools/library/Basics_of_Event_Management/

http://tseng-blog.nge-web.net/blog/2009/02/17/how-implement-your-own-listener-android-java/

http://tseng-blog.nge-web.net/blog/2009/02/17/how-implement-your-own-listener-android-java/

ANSWER TO YOUR QUESTION: You will need to write a FUNCTION for the code implementation and call it in your onClick() and also in onCreate()

回答您的问题:您需要为代码实现编写一个函数,并在 onClick() 和 onCreate() 中调用它

回答by Arun Badole

Put this line in your code

将此行放在您的代码中

b1.performClick();

in onCreate() Method.

在 onCreate() 方法中。

回答by MoraRockey

final Timer t = new Timer();
t.schedule(new TimerTask() {

    @Override
    public void run() {
        // Your Actions
        t.cancel();
    }

},5000);

After 5 seconds (5000 ms) action will perform.

5 秒(5000 毫秒)后将执行操作。

回答by Jigar Patel

For that you have to call listener on the app, eg:

为此,您必须在应用程序上调用侦听器,例如:

onCreate()
{

    button.setOnclickListner(this);
}

and put action on:

并采取行动:

    button.setOnclickListner(this);
   {
         #action
   }