Android(安卓)ToggleButton 控件
ToggleButton 将 checked/unchecked(选中/未选中)状态显示为按钮。它基本上是一个带指示灯 on/off(开/关)的按钮。
ToggleButton 属性
以下是与 ToggleButton 控件相关的重要属性。您可以查看 Android 官方文档,以获取完整的属性列表和可用于更改这些属性的相关方法。
| 编号 | 属性 & 描述 |
|---|---|
| 1 | android:disabledAlpha 禁用时应用于指示器的阿尔法。 |
| 2 | android:textOff 按钮未选中时的文本。 |
| 3 | android:textOn 选中按钮时的文本。 |
继承自 android.widget.TextView 类:
| 编号 | 属性 & 描述 |
|---|---|
| 1 | android:autoText 如果设置,则指定此 TextView 具有文本输入方法,并自动更正一些常见的拼写错误。 |
| 2 | android:drawableBottom 要在文本下方绘制的绘图。 |
| 3 | android:drawableRight 要绘制到文本右侧的绘图。 |
| 4 | android:editable 如果设置,则指定此 TextView 具有输入方法。 |
| 5 | android:text 要显示的文本。 |
继承自 android.view.View 类:
| 编号 | 属性 & 描述 |
|---|---|
| 1 | android:background 这是一个可作为背景使用的绘图。 |
| 2 | android:content描述 定义了简要描述视图内容的文本。 |
| 3 | android:id 提供了此视图的标识符名称。 |
| 4 | android:onClick 单击视图时要调用的此视图上下文中方法的名称。 |
| 5 | android:visibility 控制视图的初始可见性。 |
实例
这个实例将带您完成简单的步骤,展示如何使用线性布局和切换按钮创建自己的 Android 应用程序。
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用 Android studio IDE 创建一个 Android 应用程序,并将其命名为 com.example.saira_000.myapplication 包下的 myapplication,如 Hello World 实例 一章所述。 |
| 2 | 修改 src/MainActivity.java 文件添加点击事件。 |
| 3 | 修改 res/layout/activity_main.xml 文件的默认内容来包含 Android UI 控件。 |
| 4 | 无需声明默认常量 Android studio 负责 string.xml 中的默认常量 |
| 5 | 运行应用程序以启动 Android 模拟器并验证应用程序中所做更改的结果。 |
以下是修改后的主活动文件 src/MainActivity.java 的内容。此文件可以包括每个基本生命周期方法。
在下面的实例中,abc 表示图像:
package com.example.saira_000.myapplication;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import android.widget.ToggleButton;public class MainActivity extends ActionBarActivity {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() {@Overridepublic void onClick(View v) {StringBuffer result = new StringBuffer();result.append("You have clicked first ON Button-:) ").append(tg1.getText());result.append("You have clicked Second ON Button -:) ").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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tutorials point"android:textColor="#ff87ff09"android:textSize="30dp"android:layout_above="@+id/imageButton"android:layout_centerHorizontal="true"android:layout_marginBottom="40dp" /><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageButton"android:src="@drawable/abc"android:layout_centerVertical="true"android:layout_centerHorizontal="true" /><ToggleButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="On"android:id="@+id/toggleButton"android:checked="true"android:layout_below="@+id/imageButton"android:layout_toEndOf="@+id/button2/><ToggleButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Off"android:id="@+id/toggleButton2"android:checked="true"android:layout_alignTop="@+id/toggleButton" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button2"android:text="ClickMe"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true" /></RelativeLayout>
下面是 res/values/strings.xml 的内容定义了新常量:
<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">My Application</string></resources>
下面是 AndroidManifest.xml 的内容:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.saira_000.myapplication" ><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.My Application.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
让我们尝试运行 MyApplication 应用程序。我假设您在进行环境设置时创建了 AVD。要从 Android Studio 运行应用程序,请打开项目的 activity 文件之一,然后单击工具栏上的运行 Eclipse
图标。Android studio 在您的 AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口:

如果你先点击按钮,你会在点击第一个 on 按钮时收到一条的消息,或者如果你点击第二个 on 按钮,你就会在点击第二次 on 按钮时得到一条信息
练习
建议在编程时使用布局 XML 文件中的 ToggleButton 的不同属性来尝试上面的示例,以使 ToggleButton 具有不同的外观。尝试使其可编辑,更改为字体颜色、字体系列、宽度、文本大小等,然后查看结果。您还可以在一个活动中使用多个 ToggleButton 控件来尝试上面的实例。