问题
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