Originally a checkbox can be used, but now the new needs to add a state is disabled,disabled state to show a particular picture, this checkbox can not solve, and the state is not good control, so simply package a forget.
Define a custom Attrs first:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Imageviewcheckbox" > <attr name= "default_state" format= "integer"/> <attr name= "checked_bkg" format= "Reference"/ > <attr name= "unchecked_bkg" format= "reference"/> <attr name= "checked_disabled" format= " Reference "/> </declare-styleable></resources>
And then write this custom ImageView.
Package Com.amuro.main;import Com.amuro.imageviewcheckboxtest.r;import Android.content.context;import Android.content.res.typedarray;import Android.util.attributeset;import Android.view.view;import Android.widget.imageview;public class Imageviewcheckbox extends imageview{public static final int check_state_disabled = 0;public static final int check_state_unchecked = 1;public static final int check_state_checked = 2;private int CHECK_BK g_id;private int uncheck_bkg_id;private int disable_check_bkg_id;private int checkstate;public ImageViewCheckBox ( Context context) {This (context, null);} Public Imageviewcheckbox (context context, AttributeSet Attrs) {This (context, attrs, 0);} Public Imageviewcheckbox (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); Init (attrs);} private void init (AttributeSet attrs) {TypedArray t = getcontext (). Obtainstyledattributes (Attrs, R.styleable.imageviewcheckbox); CheckState = T.getinteger (r.styleable.imageviewcheckbox_default_stAte, check_state_unchecked); check_bkg_id = T.getresourceid (r.styleable.imageviewcheckbox_checked_bkg, 0); Uncheck_ bkg_id = T.getresourceid (r.styleable.imageviewcheckbox_unchecked_bkg, 0);d isable_check_bkg_id = T.getResourceId ( r.styleable.imageviewcheckbox_checked_disabled, 0); Setbkgbycheckstate (); Setonclicklistener (new OnClickListener () {@Overridepublic void OnClick (View v) {changestate ();}}); T.recycle ();} public void Changestate () {if (CheckState = = check_state_disabled) {return;} if (CheckState = = check_state_unchecked) {checkstate = check_state_checked;} else if (CheckState = = check_state_checked) {checkstate = check_state_unchecked;} Setbkgbycheckstate (); Notifylistner ();} public void setcheckdisabled () {this.checkstate = Check_state_disabled;setbkgbycheckstate ();} private void Setbkgbycheckstate () {if (CheckState = = check_state_unchecked) {setbackgroundresource (uncheck_bkg_id);} else if (CheckState = = check_state_disabled) {setbackgroundresource (disable_check_bkg_id);} Else{setbackgroundresource (check_bkg_id);}} public interface Oncheckstatechangedlistener{public void Oncheckstatechanged (Boolean isChecked); Private Oncheckstatechangedlistener listener;public void Setoncheckstatechangedlistener ( Oncheckstatechangedlistener listener) {This.listener = listener;} private void Notifylistner () {if (This.listener! = null) {if (CheckState = = check_state_unchecked) { This.listener.onCheckStateChanged (false);} else if (CheckState = = check_state_checked) {this.listener.onCheckStateChanged (true);}}}}ImageView directly encapsulated the Click event, the corresponding picture in various states can be directly read from the configuration file, the user click on the image directly to the switch simulation checkbox effect, disable state Changestate method will be directly invalid. The callback form and the checkbox, the user will feel very cordial, haha.
The last time you use it, configure this imageview in XML:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:app=" Http://schemas.android.com/apk/res/com.amuro.imageviewcheckboxtest " android:layout _width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > < Com.amuro.main.ImageViewCheckBox android:id= "@+id/ivcb" android:layout_width= "40DP" android:layout _height= "40DP" app:default_state= "1" app:checked_bkg= "@drawable/paysdk2_icon_virtual_ticket_selected" app:unchecked_bkg= "@drawable/paysdk2_icon_virtual_ticket_unselected" app:checked_disabled= "@ Drawable/paysdk2_icon_virtual_ticket_select_disabled " /></linearlayout>
Above, thanks for watching ~
Customizing the ImageView of a check picture for dynamic settings replaces a checkbox