Android ToggleButton 控件

  • Android ToggleButton 控件

    Android ToggleButton 将已选中/未选中状态显示为按钮。它基本上是一个带有指示灯的开/关按钮。
  • ToggleButton 属性

    以下是与ToggleButton控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。
    属性 说明
    android:disabledAlpha 这是禁用时应用于指标的Alpha值。
    android:textOff 这是未选中按钮时的文本。
    android:textOn 这是选中按钮时的文字。
    继承自android.widget.TextView类-
    属性 说明
    android:autoText 如果设置,则指定此TextView具有文本输入法,并自动纠正一些常见的拼写错误。
    android:drawableBottom 这是要在文本下方绘制的图形。
    android:drawableRight 这是要在文本右侧绘制的图形。
    android:editable 如果设置,则指定此TextView具有输入法。
    android:text 这是要显示的文本。
    继承自android.view.View类-
    属性 说明
    android:background 这是一个可绘制的背景。
    android:contentDescription 这定义了简短描述视图内容的文本。
    android:id 这将为此视图提供一个标识符名称,
    android:onClick 这是单击该视图时在该视图上下文中要调用的方法的名称。
    android:visibility 这控制了视图的初始可见性。
  • 示例

    本示例将带您完成一些简单的步骤,使用ToggleButton创建自己的Android应用程序。
    1. 您将使用Android Studio创建一个Android应用程序,并将其命名为Demo,位于com.jc2182.demo包下,如Hello World示例一章中所述。
    2. 修改src/MainActivity.java文件以添加click事件。
    3. 修改res/layout/activity_main.xml文件的默认内容以包括Android UI控件。
    4. 运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。
    以下是修改后的主要活动文件src/com.jc2182.demo/MainActivity.java的内容。该文件可以包括每个基本生命周期方法。
    
    package com.jc2182.demo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    import android.widget.ToggleButton;
    
    public class MainActivity extends Activity {
    
        ToggleButton tg1,tg2;
        Button b1;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tg1=(ToggleButton)findViewById(R.id.toggleButton);
            tg2=(ToggleButton)findViewById(R.id.toggleButton2);
    
            b1=(Button)findViewById(R.id.button2);
            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    StringBuffer result = new StringBuffer();
                    result.append("第一个开关 :::) ").append(tg1.getText());
                    result.append("\n");
                    result.append("第二个开关 :::) ").append(tg2.getText());
                    Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    
    以下是res/layout/activity_main.xml文件的内容-
    
    <RelativeLayout 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=".MainActivity">
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蝴蝶教程"
            android:textColor="#ff87ff09"
            android:textSize="30dp"
            android:layout_above="@+id/imageButton"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp" />
    
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton"
            android:src="@drawable/logo"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    
        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton"
            android:layout_marginStart="39dp"
            android:layout_marginTop="29dp"
            android:layout_toEndOf="@+id/button2"
            android:checked="true"
            android:text="On" />
    
        <ToggleButton
            android:id="@+id/toggleButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/toggleButton"
            android:layout_alignParentStart="true"
            android:layout_marginStart="30dp"
            android:layout_marginTop="-4dp"
            android:checked="true"
            android:text="Off" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="59dp"
            android:text="点击我" />
    
    </RelativeLayout>
    
    让我们尝试运行刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后工具栏中单击“运行”图标。Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面-
    点击切换开关,并点击“点击我”按钮时候如下所示。
    建议在编程时尝试上述示例,在Layout XML文件中使用ToggleButton的不同属性,以使ToggleButton具有不同的外观。尝试使其可编辑,更改为字体颜色,字体系列,宽度,字体大小等,然后查看结果。您也可以尝试在一个活动中使用多个ToggleButton控件的示例。