Skip to content

Commit

Permalink
XCheckBox
Browse files Browse the repository at this point in the history
  • Loading branch information
pichsy committed Mar 4, 2021
1 parent 9c0b2ca commit 88466f7
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 271 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation project(':widget')
implementation 'com.github.xujiaji:ripple-checkbox:0.0.5'
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/pichs/app/xwidget/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pichs.common.widget.cardview.XCardButton;
import com.pichs.common.widget.checkbox.XCheckBox;
import com.pichs.common.widget.switcher.XSwitchButton;
import com.pichs.common.widget.utils.XStatusBarHelper;
import com.pichs.common.widget.utils.XTypefaceHelper;
Expand All @@ -28,8 +32,32 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

XCardButton btn = findViewById(R.id.btn1);
XCheckBox checkbox = findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new XCheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isChecked) {
Toast.makeText(getApplicationContext(), "ischecked:" + isChecked, Toast.LENGTH_LONG).show();
}
});


XSwitchButton swb = findViewById(R.id.swb);
swb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {
checkbox.setImageDrawable(new ColorDrawable(Color.BLACK));
checkbox.setCheckedDrawable(new ColorDrawable(Color.RED));

} else {
checkbox.setImageDrawable(new ColorDrawable(Color.GREEN));
checkbox.setCheckedDrawable(new ColorDrawable(Color.BLUE));

}

}
});
swb.setThumbColor(Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK);
swb.setBackgroundColor(Color.GREEN, Color.CYAN);
btn.setOnClickListener(new View.OnClickListener() {
Expand Down
55 changes: 44 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

<LinearLayout
android:layout_width="match_parent"
android:background="#0ff"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:background="#0ff">

<com.pichs.common.widget.space.XStatusBarSpace
android:layout_width="match_parent"
Expand Down Expand Up @@ -103,25 +103,58 @@


<com.pichs.common.widget.input.InputLayout
android:text="十大发牢骚"
android:textColor="#f00"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="卫生间"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:hint="卫生间"
android:text="十大发牢骚"
android:textColor="#f00"
android:textColorHint="#999"
android:textSize="18sp"
app:xp_clearIconVisible="true"
app:xp_eyeOpenDrawable="#99f"
android:textStyle="bold"
app:xp_clearDrawable="#BD0CE8"
app:xp_clearIconVisible="false"
app:xp_eyeCloseDrawable="#0f8"
app:xp_eyeIconVisible="true"
app:xp_clearDrawable="#BD0CE8"
app:xp_eyeOpenDrawable="#99f" />


<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"/>
android:layout_height="80dp"
android:background="#D1EFE3"
android:orientation="vertical">

<com.pichs.common.widget.checkbox.XSmoothCheckBox
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
app:xp_cbox_color_unchecked_stroke="#0ff" />

<com.pichs.common.widget.checkbox.XCheckBox
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/checkbox"
android:layout_gravity="center"
android:src="#DDD7E0"
app:xp_checked="true"
app:xp_radius="15dp"
app:xp_src_checked="#EF0ADC" />

</LinearLayout>


<androidx.appcompat.widget.AppCompatCheckBox
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="10dp" />

<EditText
android:layout_width="match_parent"
android:hint="师范生克拉夫"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:hint="师范生克拉夫" />

<Space
android:layout_width="match_parent"
Expand Down
115 changes: 115 additions & 0 deletions widget/src/main/java/com/pichs/common/widget/checkbox/XCheckBox.java
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);
}
}
53 changes: 19 additions & 34 deletions widget/src/main/java/com/pichs/common/widget/input/InputLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class InputLayout extends XCardLinearLayout {
// 图标大小
private int clearIconWidth, clearIconHeight;
private int eyeIconWidth, eyeIconHeight;

private CharSequence text = "";
private CharSequence hintText = "";
private int eyeIconColorFilter, clearIconColorFilter;
Expand All @@ -85,6 +86,10 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
mEditText = findViewById(R.id.et_content);
mClearImageView = findViewById(R.id.iv_clear);
mEyeImageView = findViewById(R.id.iv_eye);
eyeIconWidth = XDisplayHelper.dp2px(context, 30);
eyeIconHeight = eyeIconWidth;
clearIconWidth = XDisplayHelper.dp2px(context, 26);
clearIconHeight = clearIconWidth;

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputLayout, defStyleAttr, 0);
int indexCount = ta.getIndexCount();
Expand All @@ -106,13 +111,13 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
} else if (index == R.styleable.InputLayout_xp_eyeIcon_padding) {
eyeIconPadding = ta.getDimensionPixelSize(index, eyeIconPadding);
} else if (index == R.styleable.InputLayout_xp_eyeIcon_width) {
eyeIconWidth = ta.getDimensionPixelSize(index, XDisplayHelper.dp2px(context, 30));
eyeIconWidth = ta.getDimensionPixelSize(index, eyeIconWidth);
} else if (index == R.styleable.InputLayout_xp_eyeIcon_height) {
eyeIconHeight = ta.getDimensionPixelSize(index, XDisplayHelper.dp2px(context, 30));
eyeIconHeight = ta.getDimensionPixelSize(index, eyeIconHeight);
} else if (index == R.styleable.InputLayout_xp_clearIcon_height) {
clearIconHeight = ta.getDimensionPixelSize(index, XDisplayHelper.dp2px(context, 28));
clearIconHeight = ta.getDimensionPixelSize(index, clearIconHeight);
} else if (index == R.styleable.InputLayout_xp_clearIcon_width) {
clearIconWidth = ta.getDimensionPixelSize(index, XDisplayHelper.dp2px(context, 28));
clearIconWidth = ta.getDimensionPixelSize(index, clearIconWidth);
} else if (index == R.styleable.InputLayout_xp_clearIcon_padding) {
clearIconPadding = ta.getDimensionPixelSize(index, clearIconPadding);
} else if (index == R.styleable.InputLayout_android_text) {
Expand Down Expand Up @@ -157,15 +162,13 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
setEyeIconVisible(isEyeIconShow);
setEyeCloseDrawable(mEyeCloseDrawable);
setEyeOpenDrawable(mEyeOpenDrawable);
setEyeIconHeight(eyeIconHeight);
setEyeIconWidth(eyeIconWidth);
setEyeIconSize(eyeIconWidth,eyeIconHeight);
setEyeIconPadding(eyeIconPadding);
setEyeIconColorFilter(eyeIconColorFilter);

setClearDrawable(mClearDrawable);
setClearIconVisible(isClearIconShow);
setClearIconHeight(clearIconHeight);
setClearIconWidth(clearIconWidth);
setClearIconSize(clearIconWidth,clearIconHeight);
setClearIconPadding(clearIconPadding);
setCleaIconColorFilter(clearIconColorFilter);
handleViewData();
Expand Down Expand Up @@ -257,17 +260,6 @@ public InputLayout setClearDrawable(Drawable clearDrawable) {
return this;
}

public InputLayout setClearIconHeight(int clearIconHeight) {
this.clearIconHeight = clearIconHeight;
if (mClearImageView != null) {
ViewGroup.LayoutParams layoutParams = mClearImageView.getLayoutParams();
if (layoutParams != null) {
layoutParams.height = clearIconHeight;
mClearImageView.setLayoutParams(layoutParams);
}
}
return this;
}

public InputLayout setClearIconPadding(int clearIconPadding) {
this.clearIconPadding = clearIconPadding;
Expand All @@ -277,11 +269,13 @@ public InputLayout setClearIconPadding(int clearIconPadding) {
return this;
}

public InputLayout setClearIconWidth(int clearIconWidth) {
this.clearIconWidth = clearIconWidth;
public InputLayout setClearIconSize(int width,int height) {
this.clearIconHeight = height;
this.clearIconWidth = width;
if (mClearImageView != null) {
ViewGroup.LayoutParams layoutParams = mClearImageView.getLayoutParams();
if (layoutParams != null) {
layoutParams.height = clearIconHeight;
layoutParams.width = clearIconWidth;
mClearImageView.setLayoutParams(layoutParams);
}
Expand All @@ -295,23 +289,14 @@ public InputLayout setDisableCopyAndPaste(boolean disableCopyAndPaste) {
return this;
}

public InputLayout setEyeIconWidth(int eyeIconWidth) {
this.eyeIconWidth = eyeIconWidth;
if (mEyeImageView != null) {
ViewGroup.LayoutParams layoutParams = mEyeImageView.getLayoutParams();
if (layoutParams != null) {
layoutParams.width = eyeIconWidth;
mEyeImageView.setLayoutParams(layoutParams);
}
}
return this;
}

public InputLayout setEyeIconHeight(int eyeIconHeight) {
this.eyeIconHeight = eyeIconHeight;
public InputLayout setEyeIconSize(int width, int height) {
this.eyeIconWidth = width;
this.eyeIconHeight = height;
if (mEyeImageView != null) {
ViewGroup.LayoutParams layoutParams = mEyeImageView.getLayoutParams();
if (layoutParams != null) {
layoutParams.width = eyeIconWidth;
layoutParams.height = eyeIconHeight;
mEyeImageView.setLayoutParams(layoutParams);
}
Expand Down
Loading

0 comments on commit 88466f7

Please sign in to comment.