Android 復選框示例含代碼

    Android 復選框示例

    Android CheckBox 是一種具有兩種狀態(選中或未選中)的按鈕。愛掏網 - it200.com

    復選框的用途可以很多。愛掏網 - it200.com例如,可以用來了解用戶的興趣愛好,激活/關閉特定的操作等。愛掏網 - it200.com

    Android CheckBox類是CompoundButton類的子類。愛掏網 - it200.com

    android.widget.CheckBox類提供了創建復選框的功能。愛掏網 - it200.com

    CheckBox類的方法

    CheckBox類繼承了View、TextView和Button類的許多方法。愛掏網 - it200.com其中一些如下:

    方法 描述
    public boolean isChecked() 如果被選中則返回true,否則返回false。愛掏網 - it200.com
    public void setChecked(boolean status) 改變CheckBox的狀態。愛掏網 - it200.com

    Android CheckBox 示例

    activity_main.xml

    將三個復選框和一個按鈕拖到布局中。愛掏網 - it200.com現在,activity_main.xml 文件將如下所示:

    <?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.checkbox.MainActivity">  
    
    
        <CheckBox  
            android:id="@+id/checkBox"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="144dp"  
            android:layout_marginTop="68dp"  
            android:text="Pizza"  
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintTop_toTopOf="parent" />  
    
        <CheckBox  
            android:id="@+id/checkBox2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="144dp"  
            android:layout_marginTop="28dp"  
            android:text="Coffee"  
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintTop_toBottomOf="@+id/checkBox" />  
    
        <CheckBox  
            android:id="@+id/checkBox3"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="144dp"  
            android:layout_marginTop="28dp"  
            android:text="Burger"  
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintTop_toBottomOf="@+id/checkBox2" />  
    
        <Button  
            android:id="@+id/button"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="144dp"  
            android:layout_marginTop="184dp"  
            android:text="Order"  
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintTop_toBottomOf="@+id/checkBox3" />  
    
    </android.support.constraint.ConstraintLayout>  
    

    Activity類

    讓我們編寫代碼來檢查哪個切換按鈕是打開/關閉的。愛掏網 - it200.com

    package example.javatpoint.com.checkbox;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        CheckBox pizza,coffe,burger;
        Button buttonOrder;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            addListenerOnButtonClick();
        }
        public void addListenerOnButtonClick(){
            //Getting instance of CheckBoxes and Button from the activty_main.xml file
            pizza=(CheckBox)findViewById(R.id.checkBox);
            coffe=(CheckBox)findViewById(R.id.checkBox2);
            burger=(CheckBox)findViewById(R.id.checkBox3);
            buttonOrder=(Button)findViewById(R.id.button);
    
            //Applying the Listener on the Button click
            buttonOrder.setOnClickListener(new View.OnClickListener(){
    
                @Override
                public void onClick(View view) {
                    int totalamount=0;
                    StringBuilder result=new StringBuilder();
                    result.append("Selected Items:");
                    if(pizza.isChecked()){
                        result.append("\nPizza 100Rs");
                        totalamount+=100;
                    }
                    if(coffe.isChecked()){
                        result.append("\nCoffe 50Rs");
                        totalamount+=50;
                    }
                    if(burger.isChecked()){
                        result.append("\nBurger 120Rs");
                        totalamount+=120;
                    }
                    result.append("\nTotal: "+totalamount+"Rs");
                    //Displaying the message on the toast
                    Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
                }
    
            });
        }
    }
    

    輸出:

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 国产凹凸在线一区二区| 日韩一区二区超清视频| 波多野结衣在线观看一区 | 波多野结衣一区二区三区高清在线 | 亚洲综合一区二区三区四区五区| 中文字幕国产一区| 国产精品美女一区二区| 3d动漫精品啪啪一区二区免费| 久久久久国产一区二区| 无码AV动漫精品一区二区免费 | 国产成人精品一区二区A片带套| 国产一区二区三区在线观看精品| 精品国产免费观看一区| 中字幕一区二区三区乱码 | 麻豆一区二区三区蜜桃免费 | 久久se精品一区二区| 国产福利电影一区二区三区久久久久成人精品综合 | 久久国产精品最新一区| 99精品国产一区二区三区| 国产在线不卡一区| 在线精品自拍亚洲第一区| 国产成人一区二区三区| 欧美日韩一区二区成人午夜电影| 国产精品自拍一区| 亚洲一区二区三区在线观看网站| 亚洲一区在线视频观看| 日本视频一区在线观看免费| 精品视频一区二区三三区四区| 无码国产精品一区二区免费vr| 另类免费视频一区二区在线观看| 国产精品高清一区二区三区| 亚洲AV无码一区二区三区系列| 国产日韩一区二区三区在线观看| 国产精品无码AV一区二区三区| 亚洲成AV人片一区二区| 精品无码av一区二区三区| 亚洲熟妇无码一区二区三区导航| 日本片免费观看一区二区| 欧美日韩精品一区二区在线观看| 精品免费国产一区二区三区| 免费一区二区无码视频在线播放|