Skip to content

Commit

Permalink
fix xcheckbox bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pichsy committed Mar 17, 2021
1 parent a9c8f2d commit cfcf9b4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 29 deletions.
7 changes: 6 additions & 1 deletion app/src/main/java/com/pichs/app/xwidget/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@
<com.pichs.common.widget.checkbox.XCheckBox
android:layout_width="44dp"
android:layout_height="44dp"
android:src="#00f"
android:id="@+id/xchekbox"
app:xp_radius="22dp"
android:layout_marginTop="30dp"
android:src="#9D9DC4"
android:layout_marginBottom="30dp"
app:xp_checked="true"
app:xp_clickable="false"
app:xp_src_checked="#f00" />
app:xp_src_checked="#6532E6" />

<!-- <com.pichs.common.widget.input.InputLayout-->
<!-- android:layout_width="match_parent"-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
}
}
Expand All @@ -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;
}
Expand Down

0 comments on commit cfcf9b4

Please sign in to comment.