From cfcf9b4d4b0fd786faae85d4a113025fce25bf53 Mon Sep 17 00:00:00 2001 From: pichsy Date: Wed, 17 Mar 2021 21:17:54 +0800 Subject: [PATCH] fix xcheckbox bug --- .../com/pichs/app/xwidget/MainActivity.java | 7 +- app/src/main/res/layout/activity_main.xml | 8 +- .../common/widget/checkbox/XCheckBox.java | 91 +++++++++++++------ 3 files changed, 77 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/pichs/app/xwidget/MainActivity.java b/app/src/main/java/com/pichs/app/xwidget/MainActivity.java index 3d703e8..38d14c1 100644 --- a/app/src/main/java/com/pichs/app/xwidget/MainActivity.java +++ b/app/src/main/java/com/pichs/app/xwidget/MainActivity.java @@ -26,13 +26,16 @@ public class MainActivity extends AppCompatActivity { XRoundTextView xrv; - + XCheckBox xchekbox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); XStatusBarHelper.translucent(this); XStatusBarHelper.setStatusBarDarkMode(this); setContentView(R.layout.activity_main); + xchekbox = findViewById(R.id.xchekbox); + + xrv = findViewById(R.id.tv_round); @@ -89,6 +92,8 @@ protected void onCreate(Bundle savedInstanceState) { } public void changeRadius(View view) { + xchekbox.setCanClick(!xchekbox.isCanClick()); + xrv.setRadius(XDisplayHelper.dp2px(this, 38)); // xrv.setBorderWidth(XDisplayHelper.dp2px(this, 3)); // xrv.setBorderColor(Color.RED); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0148a43..ec7f49b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -105,10 +105,14 @@ + app:xp_src_checked="#6532E6" /> diff --git a/widget/src/main/java/com/pichs/common/widget/checkbox/XCheckBox.java b/widget/src/main/java/com/pichs/common/widget/checkbox/XCheckBox.java index 98a5e6d..cac5466 100644 --- a/widget/src/main/java/com/pichs/common/widget/checkbox/XCheckBox.java +++ b/widget/src/main/java/com/pichs/common/widget/checkbox/XCheckBox.java @@ -4,6 +4,7 @@ import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.util.AttributeSet; +import android.util.Log; import android.view.View; import android.widget.Checkable; @@ -18,7 +19,7 @@ public class XCheckBox extends XCardImageView implements Checkable, View.OnClick private Drawable checkedDrawable; private Drawable normalDrawable; private boolean isChecked = false; - private boolean isClickable = true; + private boolean mCanClick = true; public XCheckBox(@NonNull Context context) { this(context, null); @@ -36,38 +37,70 @@ public XCheckBox(@NonNull Context context, @Nullable AttributeSet attrs, int def private void initThis(Context context, AttributeSet attrs, int defStyleAttr) { if (attrs != null) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.XCheckBox); - checkedDrawable = ta.getDrawable(R.styleable.XCheckBox_xp_src_checked); - normalDrawable = ta.getDrawable(R.styleable.XCheckBox_android_src); - isChecked = ta.getBoolean(R.styleable.XCheckBox_xp_checked, false); - isClickable = ta.getBoolean(R.styleable.XCheckBox_xp_clickable, true); + this.checkedDrawable = ta.getDrawable(R.styleable.XCheckBox_xp_src_checked); + this.normalDrawable = ta.getDrawable(R.styleable.XCheckBox_android_src); + this.isChecked = ta.getBoolean(R.styleable.XCheckBox_xp_checked, false); + this.mCanClick = ta.getBoolean(R.styleable.XCheckBox_xp_clickable, true); ta.recycle(); - setChecked(isChecked); } - super.setOnClickListener(this); + Log.d("XCheckBox", "mClickable: " + mCanClick); + setChecked(this.isChecked); super.setClickable(true); + super.setOnClickListener(this); } public void setCheckedDrawable(Drawable checkedDrawable) { this.checkedDrawable = checkedDrawable; - if (isChecked) { + if (this.isChecked) { super.setImageDrawable(this.checkedDrawable); } } + /** + * 是否开启可点击 + * + * @param canClick canClick + */ + public final void setCanClick(boolean canClick) { + this.mCanClick = canClick; + } + + /** + * isCanClick + * + * @return isCanClick + */ + public final boolean isCanClick() { + return this.mCanClick; + } + + /** + * 请使用 {@link #setCanClick(boolean)} + * + * @param clickable clickable + * @hide {@link #setClickable(boolean)} + */ + @Deprecated @Override - public void setClickable(boolean clickable) { - isClickable = clickable; + public final void setClickable(boolean clickable) { + super.setClickable(clickable); } + /** + * 请使用 {@link #isCanClick()} + * + * @hide {@link #isClickable()} + */ + @Deprecated @Override - public boolean isClickable() { - return isClickable; + public final boolean isClickable() { + return super.isClickable(); } @Override public void setImageDrawable(@Nullable Drawable drawable) { this.normalDrawable = drawable; - if (!isChecked) { + if (!this.isChecked) { super.setImageDrawable(drawable); } } @@ -81,44 +114,50 @@ public void setNormalImageDrawable(@Nullable Drawable drawable) { */ @Deprecated @Override - public void setOnClickListener(@Nullable OnClickListener l) { - // 禁止设置点击事件 - // super.setOnClickListener(l); + public final void setOnClickListener(@Nullable OnClickListener l) { } @Override public void setChecked(boolean checked) { - isChecked = checked; - if (isChecked) { - super.setImageDrawable(checkedDrawable); + if (this.isChecked == checked) { + return; + } + this.isChecked = checked; + if (this.isChecked) { + super.setImageDrawable(this.checkedDrawable); } else { - super.setImageDrawable(normalDrawable); + super.setImageDrawable(this.normalDrawable); } - if (mChangeListener != null) { - mChangeListener.onCheckedChanged(isChecked); + if (this.mChangeListener != null) { + this.mChangeListener.onCheckedChanged(this.isChecked); } } @Override public boolean isChecked() { - return isChecked; + return this.isChecked; } @Override public void toggle() { - setChecked(!isChecked); + setChecked(!this.isChecked); } @Override public void onClick(View v) { - if (isClickable) { + Log.d("XCheckBox", "mCanClick: " + mCanClick); + if (this.mCanClick) { toggle(); } } - private OnCheckedChangeListener mChangeListener; + /** + * 选中时间监听 + * + * @param listener + */ public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { this.mChangeListener = listener; }