1.简单介绍
Switch是一个很简单的控件,只有两种状态,感觉只是升级版的CheckBox(但是原生的依旧很丑 )。通过实现CompoundButton.OnCheckedChangeListener接口就可以监听Switch,但是改变Switch的button属性只会在左边加一个显示按钮(如效果图一),所以就用CheckBox做了一个自定义的Switch(效果如图二,如果感觉按钮的图片太小,可以直接设置CheckBox的background属性,并且把button属性改为@null)。改变showText属性可以调节是否显示开关提示文字,textOn属性设置打开时的提示文字,TextOff属性设置关闭时的提示属性(但是好丑啊QaQ )。
2.简单实现
下面是使用Switch的示例代码。
- switch_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--开状态-->
<item android:state_checked="true" android:drawable="@drawable/switch_on"/>
<!--关状态-->
<item android:drawable="@drawable/switch_off"/>
</selector>
- activity_switch.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SwitchActivity"
android:orientation="vertical">
<!--原生Switch-->
<Switch
android:showText="true"
android:textOn="打开"
android:textOff="关闭"
android:id="@+id/sw_demo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是系统图标的Switch"
android:textSize="18sp"
/>
<!--用CheckBox自定义的Switch-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textColor="#000000"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="这是用CheckBox自定义的仿Switch"
android:gravity="left|center_vertical"
android:textSize="18sp"/>
<CheckBox
android:id="@+id/ck_switch_demo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right|center_vertical"
android:button="@drawable/switch_selector"/>
</LinearLayout>
</LinearLayout>
- SwitchActivity.java
package xyz.strasae.androidlearn.my;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class SwitchActivity extends AppCompatActivity {
Switch aSwitchDemo;
CheckBox checkBoxSwitchDemo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switch);
aSwitchDemo = findViewById(R.id.sw_demo);
checkBoxSwitchDemo = findViewById(R.id.ck_switch_demo);
aSwitchDemo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(SwitchActivity.this, String.format("你碰了了控件%s,状态为%b", compoundButton.getText().toString(), b), Toast.LENGTH_SHORT).show();
}
});
checkBoxSwitchDemo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(SwitchActivity.this, String.format("你碰了了控件%s,状态为%b", compoundButton.getText().toString(), b), Toast.LENGTH_SHORT).show();
}
});
}
}