018 Android 單選按鈕(RadioButton)和復選框(CheckBox)的使用


1.RadioButton

(1)介紹

(2)單選按鈕點擊事件的用法

(3)RadioButton與RadioGroup配合使用實現單選題功能

(4)xml布局及使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity">


    <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="您最喜歡的城市是" />

    <RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

        <RadioButton android:id="@+id/radioButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="北京" />

        <RadioButton android:id="@+id/radioButton2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上海" />

        <RadioButton android:id="@+id/radioButton3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="廣州" />

        <RadioButton android:id="@+id/radioButton4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="杭州" />
    </RadioGroup>

    <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確定" />
</LinearLayout>

2.復選框(CheckBox)

(1)介紹

 

(2)xml文件

<TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你最喜歡的運動是"/>

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="乒乓球" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="羽毛球" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="排球" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="足球" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="確定" />

(3)java后台

對應工程名:test23

package com.lucky.test23;

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.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button1;
    Button button2;
    RadioGroup radioGroup;
    CheckBox checkBox1;
    CheckBox checkBox2;
    CheckBox checkBox3;
    CheckBox checkBox4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=findViewById(R.id.button);
        button2=findViewById(R.id.button2);
        radioGroup=findViewById(R.id.radiogroup);
        checkBox1=findViewById(R.id.checkBox);
        checkBox2=findViewById(R.id.checkBox2);
        checkBox3=findViewById(R.id.checkBox3);
        checkBox4=findViewById(R.id.checkBox4);

        //綁定按鈕點擊事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i <radioGroup.getChildCount(); i++) {   //radioGroup.getChildCount()獲取子容器數量
                    RadioButton radioButton= (RadioButton) radioGroup.getChildAt(i);

                    //判斷按鈕是否被選中
                    if(radioButton.isChecked()){
                        String str=radioButton.getText().toString();
                        Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //將變量放入數組中便於取用
                CheckBox[] cbox={checkBox1,checkBox2,checkBox3,checkBox4};
                String str="";
                //遍歷數組,判斷各個復選框的選中情況
                for (int i = 0; i <cbox.length ; i++) {
                    if(cbox[i].isChecked()){
                        str=str+cbox[i].getText().toString();
                    }
                }
                Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 3.效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM