android教程之开关按钮ToggleButton

       翻翻英汉字典,我们发现Toggle其实就是开关的意思,ToggleButton有三个比较重要的属性,android:textoff,android:texton;android:checked

属性 备注
android:textoff 开关为关的时候的文字说明
android:texton 开关开的时候的文字说明
android:checked 开关的初始状态,默认为关

掌握这三个属性,其他的跟其他控件的用法差不多,具体代码如下:

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" >


    <ToggleButton 
        android:textOn="开着的"
        android:textOff="关着的"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"/>


</RelativeLayout>



原文地址:https://www.cnblogs.com/IntelligentBrain/p/5111299.html