ToggleButton与Switch

本文介绍了Android中状态开关按钮ToggleButton和Switch的使用方法及属性设置,并通过一个具体案例展示了如何利用这两种控件来切换布局方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

状态开关按钮togglebutton和开关switch

   状态开关按钮togglebutton和开关switch是由button派生出来的,本质也是按钮,支持BUtton的各种属性,从功能上看,ToggleButton、Switch与CheckBox非常的相似,他们都可以提供两种状态,不过更常用的是切换程序中的某种状态。

   android:textOn="纵向排列"       -----状态打开时显示的文本
        android:textOff="横向排列"      -----状态关闭时显示的文本
        android:checked="true"        -----开关是否打开(true:打开)

--------案例:

(1)main.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:orientation="vertical" >
    
    <!-- 定义一个ToggleButton按钮 -->
    <ToggleButton 
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="横向排列"
        android:textOn="纵向排列"
        android:checked="true"
        />
    <!-- 定义一个switch按钮 -->
    <Switch
        android:id="@+id/switcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="纵向排列"
        android:textOff="横向排列"
        android:checked="true"
        android:thumb="@drawable/toggle"
        />
    
    <LinearLayout 
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="测试按钮一"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试按钮二"
        android:textSize="15sp"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="测试按钮三"
        />
    </LinearLayout>

</LinearLayout>

 (2)MainActivity.java

package com.yby.togglebutton;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;

/**
 * 状态开关按钮togglebutton和开关switch
 * @author yby
 * description:状态开关按钮togglebutton和开关switch是由button派生出来的,
 * togglebutton和switch通常用于切换程序中的某种状态
 */

public class MainActivity  extends Activity{
    private ToggleButton tgb;
    private Switch switcher;

    /**
     * 动态控制布局
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tgb = (ToggleButton) findViewById(R.id.toggle);
        switcher = (Switch) findViewById(R.id.switcher);
        final LinearLayout test = (LinearLayout)findViewById(R.id.test);
OnCheckedChangeListener listener
= new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ //设置垂直布局 test.setOrientation(1); }else{ //设置水平布局 test.setOrientation(0); } } }; //设置监听器 tgb.setOnCheckedChangeListener(listener); switcher.setOnCheckedChangeListener(listener); } }

 (3)效果图:纵向排列和横向排列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值