实验二 单选按钮、复选按钮
一、实验目的
1.掌握Android单选按钮与复选按钮界面布局定义。
2.掌握Android复选按钮选择事件处理方法。
3.掌握Android单选按钮组的定义与事件处理方法。
二、实验内容
编程实现学生基本信息收集,并使用消息框显示,如下图1所示。


图1 初始界面和执行后的界面
三、实验仪器、设备
硬件:PC 微型计算机、8G以上内存、500G以上硬盘。
软件:Windows 7/10、Android Studio (Eclipse)、JDK、Android SDK。
四、实验步骤
1.建立Android 项目。

2.修改默认布局文件activity_main.xml,使之实现图1布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="学生基本信息" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学名:" />
<EditText
android:id="@+id/et2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美术" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育"/>
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"/>
<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旅游"/>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="提交"/>
</LinearLayout>
3.修改默认主窗口模块(MainActivity),加入收集学生基本信息,并以Toast消息框显示。
package com.sun.radiocheckbox;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private EditText et1;
private EditText et2;
private RadioButton rb1;
private RadioButton rb2;
private CheckBox cb1;
private CheckBox cb2;
private CheckBox cb3;
private CheckBox cb4;
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
cb1=(CheckBox)findViewById(R.id.cb1);
cb2=(CheckBox)findViewById(R.id.cb2);
cb3=(CheckBox)findViewById(R.id.cb3);
cb4=(CheckBox)findViewById(R.id.cb4);
btn=(Button)findViewById(R.id.btn);
}
public void onClick(View v) {
String info="学生信息\n";
info=info+"学号:"+et1.getText().toString()+"\n";
info=info+"学名:"+et2.getText().toString()+"\n";
if (rb1.isChecked()) {
info=info+"性别:"+rb1.getText().toString()+"\n";
} else {
info=info+"性别:"+rb2.getText().toString()+"\n";
}
String tmp_info="";
if (cb1.isChecked()) {
tmp_info=tmp_info+cb1.getText().toString()+"、";
}
if (cb2.isChecked()) {
tmp_info=tmp_info+cb2.getText().toString()+"、";
}
if (cb3.isChecked()) {
tmp_info=tmp_info+cb3.getText().toString()+"、";
}
if (cb4.isChecked()) {
tmp_info=tmp_info+cb4.getText().toString()+"、";
}
if((tmp_info.length()>0)&&(tmp_info.substring(tmp_info.length()-1).equals("、"))) {
tmp_info=tmp_info.substring(0,tmp_info.length()-1);
}
info=info+"爱好:"+tmp_info;
Toast.makeText(this, info, Toast.LENGTH_LONG).show();
}
}
4.启动模拟器,运行。

五、实验思考题
1.为什么单选按钮定义要使用RadioGroup?
答:一组单选按钮只能有一个被选中,为了表示这些单选按钮在一个“组”中,同组的单选按钮需要放置在一个单选按钮组的RadioGroup容器中,从而达到单选功能。
2.布局文件有若干个单选按钮但没有RadioGroup会是什么结果?
答:这些单选按钮可以同时被选中。
本文介绍了如何在Android应用中使用单选按钮和复选按钮进行界面布局,包括RadioGroup的使用以及事件处理方法,并通过实例展示了如何收集学生基本信息并用消息框显示。

534

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



