在coding的过程中需要用到简单的switch-button,因为Android自带库没有此组件,使用就打算自定义view实现一个开关按钮。
我使用了view的组合,首先思考开关按钮的组成,分为2个部分,一个是底部的圆角矩形,一部分是在开关过程中变换位置的圆。
于是写出按钮的xml布局
//首先新建一个XML布局,RelativeLayout是外圆,View是开关的内圆
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout_bg"
android:layout_width="75dp"
android:layout_height="32dp"
android:background="@drawable/bg_switch_botttom_open">
<View
android:id="@+id/view_scroll"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="@drawable/bg_switch_top_open"/>
</RelativeLayout>
//关闭时外面椭圆的背景颜色及边框颜色 bg_switch_bottom<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
<corners android:radius="32dp" />
<solid android:color="@color/bai" />
</shape>
//开启时外面椭圆的背景颜色及边框颜色 bg_switch_botttom_open
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
<corners android:radius="32dp" />
<solid android:color="@color/lvse" />
</shape>
//关闭时内小圆,也就是开关的圆点 bg_switch_top 如果关闭和开启的颜色都一样可以通用一个布局<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
<corners android:radius="8dp" />
<size
android:width="30dp"
android:height="30dp" />
<solid android:color="@android:color/white"/>
</shape>
//开启时内小圆,也就是开关的圆点 bg_switch_top_open<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
<corners android:radius="8dp" />
<size
android:width="30dp"
android:height="30dp" />
<solid android:color="@android:color/white"/>
</shape>
接下来,我们给SwitchButton自定义一个属性,defaultStatus,即默认属性,即应用启动的时候该按钮的默认状态,分为open和close。
在res/values目录下新建attrs.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SwitchButton">
<attr name="defaultStatus">
<enum name="open" value="1"/>
<enum name="close" value="0"/>
</attr>
</declare-styleable>
</resources>
接着,就可以在java代码中定义我们的SwitchButton组件 新建一个类SwitchButton.java和一个接口OnSwitchListener.java
public class SwitchButton extends RelativeLayout {
public static final int OPEN = 1;
public static final int CLOSE = 0;
private int change;
private int defaultStatus;
private int currentStatus;
private OnSwitchListener onSwitchListener;
private TypedArray typedArray;
private RelativeLayout relativeLayout;
private View view;
public SwitchButton(Context context) {
super(context);
}
public SwitchButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.layout_switch, this);
typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchButton);
initView();
init();
}
/**
* 初始化控件
*/
private void initView() {
relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout_bg);
view = findViewById(R.id.view_scroll);
}
/**
* 初始化操作
*/
private void init() {
defaultStatus = typedArray.getInt(R.styleable.SwitchButton_defaultStatus, 0);
if (defaultStatus == 0) {
relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom);
view.setBackgroundResource(R.drawable.bg_switch_top);
setViewLocation(view, RelativeLayout.ALIGN_PARENT_LEFT);
currentStatus = CLOSE;
} else if (defaultStatus == 1) {
relativeLayout.setBackgroundResource(R.drawable.bg_switch_botttom_open);
view.setBackgroundResource(R.drawable.bg_switch_top_open);
setViewLocation(view, RelativeLayout.ALIGN_PARENT_RIGHT);
currentStatus = OPEN;
}
}
private void setViewLocation(View view, int location) {
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
if (location == RelativeLayout.ALIGN_PARENT_LEFT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
} else if (location == RelativeLayout.ALIGN_PARENT_RIGHT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
}
layoutParams.addRule(location);
view.setLayoutParams(layoutParams);
}
/**
* 获取当前的状态
*/
public int getCurrentStatus() {
return currentStatus;
}
/**
* 设置状态变化监听
*/
public void setOnSwitchListener(OnSwitchListener onSwitchListener) {
this.onSwitchListener = onSwitchListener;
}
/**
* 关闭按钮
*/
private void closeButton() {
relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom);//设置大圆背景以及圆的边线颜色
view.setBackgroundResource(R.drawable.bg_switch_top);//设置小圆背景以及圆的边线颜色
setViewLocation(view, RelativeLayout.ALIGN_PARENT_LEFT);
currentStatus = CLOSE;
}
/**
* 打开按钮
*/
private void openButton() {
relativeLayout.setBackgroundResource(R.drawable.bg_switch_botttom_open);
view.setBackgroundResource(R.drawable.bg_switch_top_open);
setViewLocation(view, RelativeLayout.ALIGN_PARENT_RIGHT);
currentStatus = OPEN;
}
/**
* 改变状态
*/
private void changeStatus() {
if (currentStatus == OPEN) {
change=OPEN;
closeButton();
} else if (currentStatus == CLOSE) {
change=CLOSE;
openButton();
}
if (onSwitchListener != null) {
onSwitchListener.onSwitchChange(change); //调用监听改变时候处理逻辑的函数
}
}
//接口用于返回数据
public interface OnSwitchListener {
void onSwitchChange(int change);
}
/**
* 触摸事件
* 触摸一下,改变按钮的状态
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
/**
* 改变状态
*/
changeStatus();
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
//调用 <com.bwie.yikezhong1.ZiDingYi.SwitchButton
android:id="@+id/sw"
custom:defaultStatus="open"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//开关的点击事件
//在外部设置监听
sw.setOnSwitchListener(new SwitchButton.OnSwitchListener() {
@Override
public void onSwitchChange(int change) {
setYeJian(change);
}
});
private void setYeJian(int change) {
Drawable drawable_night_colse;
if(change==0){
drawable_night_colse = getResources().getDrawable(R.drawable.ic_yueliang2);
}else{
drawable_night_colse = getResources().getDrawable(R.drawable.ic_yueliang);
}
drawable_night_colse.setBounds(0, 0, 20, 20);//40,40为宽高
night.setCompoundDrawables(drawable_night_colse, null, null, null);
}