AndroidStudio下利用Radiogroup和RadioButton写春夏秋冬的选择

本文介绍在Android Studio 3.2环境下,如何利用Radiogroup和RadioButton组件创建一个春夏秋冬的选择功能。项目兼容API 21,作者强调了版本选择的稳定性,并提供了MainActivity文件、activity_main.xml及string.xml的相关内容。通过实例化Button对象并设置监听器,实现点击按钮时显示选中的季节。

在这里插入图片描述
在这里插入图片描述
我使用的AndroidStudio版本是3.2的,因为据说这个版本比现在最新的3.5版本的稳定。此外我在创建项目的时候兼容的最小版本为AndroidStudio5.0,api21,我下载的akp如上图所示,如果将项目搬过去出了问题,请先查看这些对不对。

以下是MainActivity文件

package com.li.a43;

import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button rbdetermine =(Button) findViewById(R.id.rbdetermine) ;
        rbdetermine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioButton spring =(RadioButton) findViewById(R.id.rbspring);
                RadioButton rbsummer =(RadioButton) findViewById(R.id.rbsummer);
                RadioButton rbautumu =(RadioButton) findViewById(R.id.rbautumu);
                RadioButton rbwinter =(RadioButton) findViewById(R.id.rbwinter);
                TextView txtResult = (TextView) findViewById(R.id.txtResult);
                //TextView txtResulta = (TextView) findViewById(R.id.txtResulta);
                //txtResult.setText("您选择的是春");

                if  (spring.isChecked()) {
                    txtResult.setText("您选择的是春");
                }
                else if(rbsummer.isChecked())
                {
                    txtResult.setText("您选择的是夏");
                }
                else if(rbautumu.isChecked())
                {
                    txtResult.setText("您选择的是秋");
                }
                else if(rbwinter.isChecked())
                {
                    txtResult.setText("您选择的是冬");
                }
                }
            });
        }
    }

以下为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=".MainActivity">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tvchoose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/choose"
            />
        <RadioButton
            android:id="@+id/rbspring"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/spring"
            />
        <RadioButton
            android:id="@+id/rbsummer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/summer"
            />
        <RadioButton
            android:id="@+id/rbautumu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/autumu"
            />
        <RadioButton
            android:id="@+id/rbwinter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/winter"
            />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >

            <RadioButton
                android:id="@+id/rbdetermine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="false"
                android:text="@string/determine"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="170dp" />

            <TextView
                android:id="@+id/txtResulta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RadioGroup>
        <TextView
            android:id="@+id/txtResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorPrimary" />
    </RadioGroup>









</android.support.constraint.ConstraintLayout>

以下为string.xml

<resources>
    <string name="app_name">4.3</string>
    <string name="spring"></string>
    <string name="summer"></string>
    <string name="autumu"></string>
    <string name="winter"></string>
    <string name="choose">请选择你最喜欢的一个季节</string>
    <string name="determine">确定</string>
</resources>

在这里插入图片描述

1.package com.li.a43;

li是创建项目时要求填的公司的名字
a43则是该项目的名字

2.Button rbdetermine =(Button) findViewById(R.id.rbdetermine) ;

这是实例化一个Button对象rbdetermine
然后对这个对象设置监听器,用来在这个按钮被点击的时候做出反应
ischedked函数用于判断RadioButton是否被选中,并输出文本
txtResult是TextView的一个实例化对象

在这里插入图片描述
以下为上传的文件
https://download.csdn.net/download/programmer9/11959425

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值