Android 切換按鈕示例含代碼

    Android 切換按鈕示例

    Android 切換按鈕 可用于在按鈕上顯示選中/未選中(開/關(guān))狀態(tài)。愛掏網(wǎng) - it200.com

    如果用戶需要在兩種狀態(tài)之間更改設(shè)置,則這非常有益。愛掏網(wǎng) - it200.com 它可用于開/關(guān)聲音、Wifi、藍(lán)牙等。愛掏網(wǎng) - it200.com

    自從 Android 4.0,還有另一種稱為“切換”的切換按鈕類型,它提供滑塊控制。愛掏網(wǎng) - it200.com

    Android ToggleButton 和 Switch 都是 CompoundButton 類的子類。愛掏網(wǎng) - it200.com

    ToggleButton 類提供了創(chuàng)建切換按鈕的功能。愛掏網(wǎng) - it200.com

    ToggleButton 類的 XML 屬性

    ToggleButton 類的 3 個 XML 屬性。愛掏網(wǎng) - it200.com

    XML 屬性 描述
    android:disabledAlpha 禁用時應(yīng)用于指示器的透明度。愛掏網(wǎng) - it200.com
    android:textOff 按鈕未選中時的文本。愛掏網(wǎng) - it200.com
    android:textOn 按鈕選中時的文本。愛掏網(wǎng) - it200.com

    ToggleButton類的方法

    ToggleButton類常用的方法如下:

    方法 描述
    CharSequence getTextOff() 返回按鈕不處于選中狀態(tài)時的文本。愛掏網(wǎng) - it200.com
    CharSequence getTextOn() 返回按鈕處于選中狀態(tài)時的文本。愛掏網(wǎng) - it200.com
    void setChecked(boolean checked) 改變此按鈕的選中狀態(tài)。愛掏網(wǎng) - it200.com

    Android ToggleButton示例

    activity_main.xml

    在布局中拖動兩個toggle按鈕和一個按鈕。愛掏網(wǎng) - it200.com現(xiàn)在activity_main.xml文件的內(nèi)容如下:

    <?xml version="1.0" encoding="utf-8"?>  
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:app="http://schemas.android.com/apk/res-auto"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        tools:context="example.javatpoint.com.togglebutton.MainActivity">  
    
        <ToggleButton  
            android:id="@+id/toggleButton"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="8dp"  
            android:layout_marginTop="80dp"  
            android:text="ToggleButton"  
            android:textOff="Off"  
            android:textOn="On"  
            app:layout_constraintEnd_toStartOf="@+id/toggleButton2"  
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintTop_toTopOf="parent" />  
    
        <ToggleButton  
            android:id="@+id/toggleButton2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginRight="60dp"  
            android:layout_marginTop="80dp"  
            android:text="ToggleButton"  
            android:textOff="Off"  
            android:textOn="On"  
            app:layout_constraintEnd_toEndOf="parent"  
            app:layout_constraintTop_toTopOf="parent" />  
    
        <Button  
            android:id="@+id/button"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginBottom="144dp"  
            android:layout_marginLeft="148dp"  
            android:text="Submit"  
            app:layout_constraintBottom_toBottomOf="parent"  
            app:layout_constraintStart_toStartOf="parent" />  
    </android.support.constraint.ConstraintLayout>  
    

    Activity類

    讓我們編寫代碼來檢查哪個切換按鈕是開/關(guān)狀態(tài)。愛掏網(wǎng) - it200.com

    package example.javatpoint.com.togglebutton;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    import android.widget.ToggleButton;
    
    public class MainActivity extends AppCompatActivity {
        private ToggleButton toggleButton1, toggleButton2;
        private Button buttonSubmit;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            addListenerOnButtonClick();
        }
    
        public void addListenerOnButtonClick(){
            //Getting the ToggleButton and Button instance from the layout xml file
            toggleButton1=(ToggleButton)findViewById(R.id.toggleButton);
            toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
            buttonSubmit=(Button)findViewById(R.id.button);
    
            //Performing action on button click
            buttonSubmit.setOnClickListener(new View.OnClickListener(){
    
                @Override
                public void onClick(View view) {
                    StringBuilder result = new StringBuilder();
                    result.append("ToggleButton1 : ").append(toggleButton1.getText());
                    result.append("\nToggleButton2 : ").append(toggleButton2.getText());
                    //Displaying the message in toast
                    Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();
                }
    
            });
    
        }
    }
    

    輸出:

    聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
    發(fā)表評論
    更多 網(wǎng)友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 亚洲字幕AV一区二区三区四区| 精品一区精品二区制服| 日韩免费视频一区| 欧美日本精品一区二区三区| 日韩成人一区ftp在线播放| 亚洲国产av一区二区三区 | 无码国产精品一区二区免费式芒果| 无码日韩人妻AV一区免费l | 国产成人精品一区二区三在线观看| 亚洲福利视频一区二区| 国产精品亚洲一区二区三区| 国产成人无码精品一区二区三区| 国产女人乱人伦精品一区二区| 日本一区二区视频| 亚洲bt加勒比一区二区| 国产精品无圣光一区二区| 福利视频一区二区牛牛| 国产福利酱国产一区二区| 亚洲综合无码一区二区三区| 国产成人精品无码一区二区三区| 无码人妻精品一区二区蜜桃AV| 日韩一区二区三区在线| 国模视频一区二区| 中文字幕AV无码一区二区三区| 人妻无码视频一区二区三区| 久久久精品人妻一区二区三区| 中文字幕一区二区三区四区| 国产高清在线精品一区小说| 日韩精品一区二区三区视频| 国产一区二区影院| 麻豆精品人妻一区二区三区蜜桃| 久久无码AV一区二区三区| 中文字幕人妻第一区| 亚洲一区精彩视频| 欧美一区内射最近更新| 午夜福利无码一区二区| 国产成人无码aa精品一区| 精品人体无码一区二区三区| 亚洲爆乳无码一区二区三区| 日本一区二区在线| 国产香蕉一区二区三区在线视频 |