android studio創建android項目(7)——RadioButton的使用

1、項目需求

  1. 本項目是一個安卓項目,啓動頁面有一個RadioGroup和一個TextView
  2. 有兩個單選按鈕,分別顯示男、女
  3. 性別默認選擇是男;TextView的文字爲:“你選擇的性別是:男”
  4. 性別選擇爲男時,TextView的文字更新爲:“你選擇的性別是:男”
  5. 性別選擇爲女時,TextView的文字更新爲:“你選擇的性別是:女”

2、項目分析

  1. RadioGroup的默認選擇
    (1)判斷默認選擇的是哪項,顯示TextView
  2. RadioGroup的點擊監聽
    (1)RadioGroup需要設置監聽器,監聽器的作用是更新TextView

3、佈局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:orientation="vertical"
    tools:context="com.example.gui.radiobuttondemo.MainActivity">

    <RadioGroup
        android:id="@+id/gender"
        android:checkedButton="@+id/male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>
        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>
    <TextView
        android:id="@+id/genderResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你選擇的性別是:"/>
</LinearLayout>

4、Activity

package com.example.gui.radiobuttondemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //聲明組件
    RadioGroup gender;
    RadioButton male;
    RadioButton female;
    TextView genderResult;
    String head; //提示的頭部
    String result;//提示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取組件
        gender=(RadioGroup)findViewById(R.id.gender);
        male=(RadioButton)findViewById(R.id.male);
        female=(RadioButton)findViewById(R.id.female);
        genderResult=(TextView)findViewById(R.id.genderResult);
        head=genderResult.getText().toString();

        /*默認選擇下提示*/
        //判斷默認選擇的id,做處理
        if(gender.getCheckedRadioButtonId()==male.getId()){
            result=head+male.getText().toString();
        }else if (gender.getCheckedRadioButtonId()==female.getId()){
            result=head+female.getText().toString();
        }
        genderResult.setText(result);

        /*監聽radiobutton的選擇*/
        gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==male.getId()){
                    result=head+male.getText().toString();
                }else if (checkedId==female.getId()){
                    result=head+female.getText().toString();
                }
                genderResult.setText(result);
            }
        });

    }
}

5、運行結果

apk下載鏈接:https://pan.baidu.com/s/19RcbQi4buM4SOUf0bwyIOA
提取碼:a8ao

運行結果:
1、啓動頁:
在這裏插入圖片描述
2、選中女
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章