Can I click a button programmatically for a predefined intent?

本秂侑毒 提交于 2019-11-27 17:33:52
Nirav Bhandari

You can click a button programmatically by using the button.performClick() method.

If your button includes any animation, you'll need to perform the click and then invalidate each step after performClick. Here's how:

 button.performClick();
 button.setPressed(true); 
 button.invalidate(); 
 button.setPressed(false); 
 button.invalidate(); 

On occasion I've also had to introduce delay to get the animation to show. Like this:

 //initiate the button
 button.performClick();
 button.setPressed(true); 
 button.invalidate(); 
 // delay completion till animation completes
 button.postDelayed(new Runnable() {  //delay button 
     public void run() {  
        button.setPressed(false); 
        button.invalidate();
        //any other associated action
     }
 }, 800);  // .8secs delay time
button.callOnClick();

this one can also be used

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