Android开发-Android常用组件-ToggleButton开关按钮 & Switch开关

4.7  开关按钮ToggleButton和开关Switch

 1.开关按钮ToggleButton

属性名

说明

android:disabledAlpha

设置按钮在禁用时的透明度

android:textOff

按钮没有被选中时显示的文字

android:textOn

按钮被选中时显示的文字 另外,除了这个我们还可以自己写个

selector,然后设置下Background属性即可

2.开关Switch

扫描二维码关注公众号,回复: 15524686 查看本文章

属性名

说明

android:showText

设置on/off的时候是否显示文字,boolean

android:splitTrack

是否设置一个间隙,让滑块与底部图片分隔,boolean

android:switchMinWidth

设置开关的最小宽度

android:switchPadding

设置滑块内文字的间隔

android:switchTextAppearance

设置开关的文字外观

android:textOff

按钮没有被选中时显示的文字

android:textOn

按钮被选中时显示的文字

android:textStyle

文字风格,粗体,斜体写划线那些

android:track

底部的图片

android:thumb

滑块的图片

android:typeface

设置字体,默认支持这三种:sans, serif, monospace;

除此以外还可以使用 其他字体文件(*.ttf)

 示例:

按钮样式:drawable文件夹下thumb_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@mipmap/caihongyuanxing"/>
    <item android:state_pressed="false" android:drawable="@mipmap/hongseyuanxing"/>
</selector>
 滑块 样式:drawable文件夹下track_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="@mipmap/yuanjiaochangfangxing"/>
    <item android:state_checked="false" android:drawable="@mipmap/hongsechangfangxing"/>
</selector>

整体效果 toggle_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_gray"
    android:gravity="center"
    android:orientation="vertical">
    <ToggleButton
        android:id="@+id/tbtn_open"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:checked="true"
        android:textOff="关闭声音"
        android:textOn="打开声音"/>
    <Switch
        android:id="@+id/swh_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:thumb="@drawable/thumb_selector"
        android:track="@drawable/track_selector"/>

</LinearLayout>

 启动文件:SwichActivity.java:

package com.example.myapplication;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SwichActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private ToggleButton tbtn_open;
    private Switch swh_status;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toggle_button);
        tbtn_open = (ToggleButton) findViewById(R.id.tbtn_open);
        swh_status = (Switch) findViewById(R.id.swh_status);
        tbtn_open.setOnCheckedChangeListener(this);
        swh_status.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(CompoundButton compoundButton,boolean b){
        switch (compoundButton.getId()){
            case R.id.tbtn_open:
                if(compoundButton.isChecked()){
                    Toast.makeText(this,"打开声音",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(this,"关闭声音",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.swh_status:
                if(compoundButton.isChecked()){
                    Toast.makeText(this,"开关:ON",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(this,"开关:OFF",Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

}
修改配置:AndroidManifest.xml:

 启动测试:

点击“打开声音”或“关闭声音”:

 点击滑块按钮:

 打开滑块开关或关闭滑块开关:

 资源:

猜你喜欢

转载自blog.csdn.net/LYly_B/article/details/129870813