前言
按照辈分来说,ToggleButton 和 Switch 都是 Button 的孙子;他们有共同的父类 CompoundButton,而 CompoundButton 是由老父亲 Button (派)生出来的。
因而,Button 支持的属性,方法很多也能应用于ToggleButton 和 Switch 。由于他两兄弟长得比较像,这里就放一起学习了。
ToggleButton
ToggleButton(开关按钮)是Android系统中比较简单的一个组件,是一个具有选中和未选中双状态的按钮,并且需要为不同的状态设置不同的显示文本。当用户在两种状态间进行切换时会触发一个 OnCheckedChange 事件。它通常用于却换程序的某种状态。
相关属性
XML属性
XML属性介绍
android:disabledAlpha 设置按钮在禁用时的透明度
android:textOff 按钮没有被选中时显示的文字
android:textOn 按钮被选中时显示的文字
此外还有一个属性较为常用:android:checked 设置该按键是否被选中,其对应的 Java方法 是 setChecked(boolean)。
示例
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Togglebutton_Activity"
android:padding="20dp"
android:orientation="vertical"
android:gravity="center|top">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:textSize="30dp" />
<!--默认对照组-->
<ToggleButton
android:id="@+id/togglebutton_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"/>
<!--设置text_on/text_off-->
<ToggleButton
android:id="@+id/togglebutton_B1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="已开启"
android:textOff="已关闭"
android:layout_marginTop="15dp"/>
<!--设置ToggleButton禁用透明度-->
<ToggleButton
android:id="@+id/togglebutton_B2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:disabledAlpha="50"
android:layout_marginTop="15dp"/>
</LinearLayout>
Java文件(OnCheckedChange事件)
package com.example.mytest_button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class Togglebutton_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_togglebutton);
final ToggleButton Oclick_toggle = findViewById(R.id.togglebutton_B);
Oclick_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(Togglebutton_Activity.this,""+ b,Toast.LENGTH_SHORT).show();
}
});
}
}
效果图
Switch
Switch 是一个可以在两种状态切换之间切换的开关控件。用户可以拖动来选择,也可以像选择复选框一样点击切换Switch的状态。状态改变时,会触发一个 OnCheckedChange 事件。
相关属性
XML属性
XML属性介绍
android:showText 设置on/off的时候是否显示文字,true / false
android:splitTrack 是否设置一个间隙,让滑块与底部图片分隔,true / false
android:switchMinWidth 设置开关的最小宽度
android:switchPadding 设置滑块内文字的间隔
android:switchTextAppearance 设置开关的文字外观
android:textOff 按钮没有被选中时显示的文字
android:textOn 按钮被选中时显示的文字
android:textStyle 文字风格,粗体,斜体写划线那些
android:track 底部的图片
android:thumb 滑块的图片
android:typeface 设置字体,默认支持这三种:sans, serif, monospace
对应 Java 方法
示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Switch_Activity"
android:padding="20dp"
android:orientation="vertical"
android:gravity="center|top">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch"
android:textSize="30dp"/>
<!--Switch默认组-->
<Switch
android:id="@+id/switch_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
<!--显示文字-->
<Switch
android:id="@+id/switch_B1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:showText="true" />
<!--设置间隙-->
<Switch
android:id="@+id/switch_B2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:splitTrack="true"/>
<!--switch最小宽度-->
<Switch
android:id="@+id/switch_B3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:switchMinWidth="80dp"/>
<!--文字外观-->
<Switch
android:id="@+id/switch_B4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:switchTextAppearance="@color/colorPrimary"
/>
<!--选中/未选中字体-->
<Switch
android:id="@+id/switch_B5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textOn="蓝牙已打开"
android:textOff="蓝牙已关闭"
android:textStyle="italic|bold" />
<!--底部图片 滑块图片-->
<Switch
android:id="@+id/switch_B7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:track="@drawable/switch2"
android:thumb="@drawable/switch1"/>
</LinearLayout>
效果图
不同安卓版本上演示有一定区别。