Clicking on ClickableSpan in CheckBox changes its state

送分小仙女□ 提交于 2019-12-12 13:21:20

问题


I've got an ImageSpan and ClickableSpan in my checkbox to display icon at the end of checkbox text and handle clicking on it. But clicking on it changes checkbox checked state, which is not needed.

How can I prevent changing checkbox state when user clicks on ClickableSpan?

Simply I need some equivalent of cancelPendingInputEvents() method for API 16+. Also I don't want to separate checkbox with checkbox and textview.


回答1:


Are you using a solution similar to that outlined in https://stackoverflow.com/a/47166879/172690? If so, I have found the following onClick implementation to be funcional:

        @Override
        public void onClick(View widget) {
            // Prevent CheckBox state from being toggled when link is clicked
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                widget.cancelPendingInputEvents();
            } else {
                if (widget instanceof CheckBox) {
                    CheckBox checkbox = (CheckBox) widget;
                    boolean state = checkBox.isChecked();
                    checkBox.post(new Runnable() {
                        @Override public void run() {
                            checkBox.setChecked(state);
                        }
                    });
                }
            }
            // Do action for link text...
        }

Essentially the state is saved during the handling of the click and then posted so it can be restored. As you can imagine, the checkbox does flash and animate while pressing the clickable spannable. However, even the cancelPendingInputEvents branch flashes the checkbox when the link is touched, so my guess is this is the best you can do short of writing your own checkbox widget.




回答2:


If you set your Checkbox text with Spannable String with the onclick listener inside the spannable sub text. All you need to do is add this line,

checkBoxView.setMovementMethod(LinkMovementMethod.getInstance());

You are good to go.



来源:https://stackoverflow.com/questions/49253115/clicking-on-clickablespan-in-checkbox-changes-its-state

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