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、选中女
在这里插入图片描述

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