CheckBoxPreference with additional Button?

China☆狼群 提交于 2019-12-14 03:12:13

问题


I'd like to show an additional button in a CheckBoxPreference. I've managed this by subclassing CheckBoxPreference and a custom layout (using this code as a basis). However, I struggle to add an OnClickListenerto this button in my PreferenceActivity. I've tried using a global OnClickListener-variable in the class extending CheckBoxPreference as well as adding a getView()-method to the PreferenceActivity that is supposed to return the Preference's view (to be able to use findViewById(R.id.my_button), but both approaches don't work :-(.


回答1:


Create an instance variable for the click listener within your subclass of CheckBoxPreference, which holds the listener you wish to set. Create a setter for this variable, calling notifyChanged() after setting the value. This will cause the onBindView method to be called on listener changes.

Now you can use this variable from within onBindView, set your listener to the button's view there (view.findViewById(R.id.yourbuttonid)).



来源:https://stackoverflow.com/questions/18090566/checkboxpreference-with-additional-button

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