Android基础控件Switch的使用

1.简单介绍

Switch是一个很简单的控件,只有两种状态,感觉只是升级版的CheckBox(但是原生的依旧很丑 )。通过实现CompoundButton.OnCheckedChangeListener接口就可以监听Switch,但是改变Switch的button属性只会在左边加一个显示按钮(如效果图一),所以就用CheckBox做了一个自定义的Switch(效果如图二,如果感觉按钮的图片太小,可以直接设置CheckBox的background属性,并且把button属性改为@null)。改变showText属性可以调节是否显示开关提示文字,textOn属性设置打开时的提示文字,TextOff属性设置关闭时的提示属性(但是好丑啊QaQ )。
图一图二

2.简单实现

下面是使用Switch的示例代码。

  1. 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>
  1. 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>
  1. 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();
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值