android button setPressed after onClick

前端 未结 7 1411
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 15:27

yesterday I noticed the possibility to integrate Fragments in older API Levels through the Compatibility package, but thats not really essential for the question. :)

7条回答
  •  伪装坚强ぢ
    2020-12-03 15:58

    Another solution is to extend Button and override setPressed(boolean pressed) method so you can handle platform calls from the onClickEvent using a flag, for example changing the boolean pressed parameter depending on your needs.

    public class MyButton extends Button {
       boolean isSelected = false;  //this is your flag
       @Override
       public void setPressed(boolean pressed) {
           if(isSelected) {
               //here you change the parameter so it stays pressed
               super.setPressed(true);
               return;
           }
           super.setPressed(pressed);
       }
    
       public void setIsSelected(boolean selected) {
           this.isSelected = selected;
       }
    
       public MyButton(Context context) {
           super(context);
       }
    }
    

提交回复
热议问题