package com.jaiky.test.statucheckbox; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.widget.ImageView; /** * Author by Jaiky, Email [email protected], Date on 5/21/2015. * PS: Not easy to write code, please indicate. */ public class ImageCheckBox 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_bkg_id; private int uncheck_bkg_id; private int disable_check_bkg_id; //当前状态 private int checkState; public ImageCheckBox(Context context) { this(context, null); } public ImageCheckBox(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ImageCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } private void init(AttributeSet attrs) { TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.ImageCheckBox); checkState = t.getInteger(R.styleable.ImageCheckBox_checked_state, CHECK_STATE_UNCHECKED); check_bkg_id = t.getResourceId( R.styleable.ImageCheckBox_checked, 0); uncheck_bkg_id = t.getResourceId( R.styleable.ImageCheckBox_unchecked, 0); disable_check_bkg_id = t.getResourceId( R.styleable.ImageCheckBox_checked_disabled, 0); setBkgByCheckState(); //如果可以点击 if (isClickable()) { setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { changeState(); } }); } t.recycle(); } public boolean isCheck(){ if (checkState == CHECK_STATE_DISABLED) { return false; } if (checkState == CHECK_STATE_CHECKED) { return true; } else { return false; } } /** * 设置选中状态 * @param check */ public void setCheck(boolean check){ if (checkState == CHECK_STATE_DISABLED) { return; } if (check) { checkState = CHECK_STATE_CHECKED; } else { checkState = CHECK_STATE_UNCHECKED; } setBkgByCheckState(); notifyListner(); } /** * 改变Check状态 */ 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) { setImageResource(uncheck_bkg_id); } else if (checkState == CHECK_STATE_DISABLED) { setImageResource(disable_check_bkg_id); } else { setImageResource(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); } } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:custom="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#bb000000" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jaiky.test.statucheckbox.MainActivity"> <com.jaiky.test.statucheckbox.ImageCheckBox android:id="@+id/cbSelect1" android:layout_width="25dp" android:layout_height="25dp" android:layout_gravity="center_vertical" android:clickable="true" custom:checked="@drawable/radiobutton_checked" custom:checked_state="1" custom:unchecked="@drawable/radiobutton_unchecked" /> <com.jaiky.test.statucheckbox.ImageCheckBox android:id="@+id/cbSelect2" android:layout_toRightOf="@id/cbSelect1" android:layout_marginLeft="30dp" android:layout_width="35dp" android:layout_height="35dp" android:layout_gravity="center_vertical" android:clickable="true" custom:checked="@drawable/radiobutton_checked" custom:checked_state="1" custom:unchecked="@drawable/radiobutton_unchecked" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/cbSelect2" android:layout_marginTop="30dp"> <com.jaiky.test.statucheckbox.ImageCheckBox android:id="@+id/cbSelect3" android:layout_width="35dp" android:layout_height="35dp" android:layout_gravity="center_vertical" android:clickable="true" custom:checked_disabled="@drawable/video_disabled" custom:checked="@drawable/video_play" custom:checked_state="1" custom:unchecked="@drawable/video_pause" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="30dp" android:text="视频按钮不可用" /> </LinearLayout> </RelativeLayout>
package com.jaiky.test.statucheckbox; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { ImageCheckBox cbSelect1, cbSelect2, cbSelect3; Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cbSelect1 = (ImageCheckBox) findViewById(R.id.cbSelect1); cbSelect2 = (ImageCheckBox) findViewById(R.id.cbSelect2); cbSelect3 = (ImageCheckBox) findViewById(R.id.cbSelect3); btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cbSelect3.setCheckDisabled(); } }); } }
欢迎转载,但请保留文章原始出处
作者:Jaiky_杰哥
出处:http://blog.csdn.net/jaikydota163/article/details/52098865