Working sample code for button.performClick()

元气小坏坏 提交于 2019-12-13 10:53:48

问题


Can someone please put a working code example of button.performClick() assigned to a OnClickListener. The following code which I have used returns false for button.performClick()-

myButton.performClick()
 myButton.setOnClickListener ( new View.OnClickListener()
      {
           @Override
           public void onClick ( View view )
           {
              .............
           }
});

回答1:


button.performClick()

should be myButton.performClick(); then you should exec it after you register the OnClickListener for your myButton

myButton.setOnClickListener ( new View.OnClickListener()
      {
           @Override
           public void onClick ( View view )
           {
              .............
           }
});


myButton.performClick();



回答2:


Declare the button Button bmi;

exampleButton=(Button)findViewById(R.id.button1);

exampleButton.setOnClickListener(new OnClickListener()
         {public void onClick
         (View  v) { 
             Intent i = new Intent(getApplicationContext(), exampleActivity.class);
             startActivity(i);}
         });

Let me know if it helped you ;)




回答3:


 final Button button= (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO handle click
            }
        });

        button.post(new Runnable() {
            @Override
            public void run() {
                button.performClick();
            }
        });


来源:https://stackoverflow.com/questions/16587549/working-sample-code-for-button-performclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!