

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

被折叠的 条评论
为什么被折叠?



