-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
233 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
widget/src/main/java/com/pichs/common/widget/checkbox/XCheckBox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.pichs.common.widget.checkbox; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.graphics.drawable.Drawable; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
import android.widget.Checkable; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.pichs.common.widget.R; | ||
import com.pichs.common.widget.cardview.XCardImageView; | ||
|
||
public class XCheckBox extends XCardImageView implements Checkable, View.OnClickListener { | ||
|
||
private Drawable checkedDrawable; | ||
private Drawable normalDrawable; | ||
private boolean isChecked = false; | ||
|
||
public XCheckBox(@NonNull Context context) { | ||
this(context, null); | ||
} | ||
|
||
public XCheckBox(@NonNull Context context, @Nullable AttributeSet attrs) { | ||
this(context, attrs, 0); | ||
} | ||
|
||
public XCheckBox(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
initThis(context, attrs, defStyleAttr); | ||
} | ||
|
||
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); | ||
ta.recycle(); | ||
setChecked(isChecked); | ||
} | ||
super.setOnClickListener(this); | ||
} | ||
|
||
public void setCheckedDrawable(Drawable checkedDrawable) { | ||
this.checkedDrawable = checkedDrawable; | ||
if (isChecked) { | ||
super.setImageDrawable(this.checkedDrawable); | ||
} | ||
} | ||
|
||
@Override | ||
public void setImageDrawable(@Nullable Drawable drawable) { | ||
this.normalDrawable = drawable; | ||
if (!isChecked) { | ||
super.setImageDrawable(drawable); | ||
} | ||
} | ||
|
||
public void setNormalImageDrawable(@Nullable Drawable drawable) { | ||
setImageDrawable(drawable); | ||
} | ||
|
||
/** | ||
* @hide 隐藏方法 | ||
*/ | ||
@Deprecated | ||
@Override | ||
public void setOnClickListener(@Nullable OnClickListener l) { | ||
// 禁止设置点击事件 | ||
// super.setOnClickListener(l); | ||
} | ||
|
||
@Override | ||
public void setChecked(boolean checked) { | ||
isChecked = checked; | ||
if (isChecked) { | ||
super.setImageDrawable(checkedDrawable); | ||
} else { | ||
super.setImageDrawable(normalDrawable); | ||
} | ||
if (mChangeListener != null) { | ||
mChangeListener.onCheckedChanged(isChecked); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isChecked() { | ||
return isChecked; | ||
} | ||
|
||
@Override | ||
public void toggle() { | ||
setChecked(!isChecked); | ||
} | ||
|
||
@Override | ||
public void onClick(View v) { | ||
toggle(); | ||
} | ||
|
||
|
||
private OnCheckedChangeListener mChangeListener; | ||
|
||
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { | ||
this.mChangeListener = listener; | ||
} | ||
|
||
|
||
public interface OnCheckedChangeListener { | ||
void onCheckedChanged(boolean isChecked); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.