一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

Android 復(fù)選框示例含代碼

Android 復(fù)選框示例

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

復(fù)選框的用途可以很多。愛掏網(wǎng) - it200.com例如,可以用來了解用戶的興趣愛好,激活/關(guān)閉特定的操作等。愛掏網(wǎng) - it200.com

Android CheckBox類是CompoundButton類的子類。愛掏網(wǎng) - it200.com

android.widget.CheckBox類提供了創(chuàng)建復(fù)選框的功能。愛掏網(wǎng) - it200.com

CheckBox類的方法

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

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

Android CheckBox 示例

activity_main.xml

將三個復(fù)選框和一個按鈕拖到布局中。愛掏網(wǎng) - it200.com現(xiàn)在,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類

讓我們編寫代碼來檢查哪個切換按鈕是打開/關(guān)閉的。愛掏網(wǎng) - 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();
            }

        });
    }
}

輸出:

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

返回頂部

主站蜘蛛池模板: 牛牛热在线视频 | 国产高清久久久 | www国产亚洲精品 | 日韩欧美中文在线 | 国产 欧美 日韩 一区 | 日韩成人在线观看 | 国产免费拔擦拔擦8x高清 | 91九色视频| 亚洲人成人一区二区在线观看 | 国产一区二区精 | 亚洲看片| 蜜桃视频在线观看免费视频网站www | 天堂一区二区三区 | 亚洲精品电影在线 | 成人在线视频一区 | 99爱国产 | 亚洲性爰 | 亚洲精品电影网在线观看 | 国产一区不卡 | 国产精品性做久久久久久 | 日本激情视频在线播放 | 一区二区三区在线免费看 | 成人激情视频 | 一区二区免费视频 | 中文字幕日韩一区 | 免费 视频 1级 | 欧美成人免费在线视频 | 成人精品在线观看 | 国产欧美性成人精品午夜 | 一区二区三区四区国产 | 日韩h | 国产一级片| 青青草精品视频 | 狠狠亚洲| 中文字幕在线免费观看 | 午夜视频免费在线观看 | 久久精品久久久久久 | 欧美成视频 | 亚洲欧美日韩一区 | 久久综合激情 | 91免费小视频 |