开发者

delay switch state on togglebutton click

开发者 https://www.devze.com 2023-03-11 10:14 出处:网络
I need to delay the switching of the state of a togglebutton when I click on it. I have to do some operation and than when another event is called the state of the togglebutton开发者_JS百科 have to ch

I need to delay the switching of the state of a togglebutton when I click on it. I have to do some operation and than when another event is called the state of the togglebutton开发者_JS百科 have to change. How can I do that? Thanks!


Subclass the ToggleButton and override the click handling. Use an AsyncTask to accomplish your task and then do the actual toggling by calling super.performClick() when you want to actually perform the toggling.

public class MyToggleButton extends ToggleButton {

    public MyToggleButton(Context context) {
        super(context);
    }

    public MyToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyToggleButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean performClick() {
        // do your thing here

            // only call the below line if you actually want it to happen.
        return super.performClick();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号