智能终端软件开发——多项选择的实现
一、实验目的
使用Checkbox控件实现多项选择
二、实验要求
1.掌握界面布局中控件设计。
2.掌握控件CheckBox的实现方法。
3.掌握利用XML布局实现多项选择界面的方法。
三、实验内容
多项选择如何实现呢?多项选择与单项选择最重要的区别就在于它可以让用户选择一个以上的选择,Android平台提供了CheckBox来实现多项选择。这里需要注意,既然用户可以选择多项,那么为了确定用户是否选择了某一项,需要对每一个选项进行事件监听。
四、实验步骤
1.创建本次实验的文件目录结构如下:

2.代码清单
3-1:firstapp/src/main/java/com.czt.fristapp/MainActivity.java
package com.czt.fristapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//用来显示题目
TextView m_TextView1;
//“提交按钮”
Button m_Button1;
//四个多选项
CheckBox m_CheckBox1;
CheckBox m_CheckBox2;
CheckBox m_CheckBox3;
CheckBox m_CheckBox4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_TextView1 = (TextView)findViewById(R.id.TextView1);
m_Button1 = (Button)findViewById(R.id.button1) ;
//取得每个CheckBox对象
m_CheckBox1 = (CheckBox) findViewById(R.id.CheckBox1);
m_CheckBox2 = (CheckBox) findViewById(R.id.CheckBox2);
m_CheckBox3 = (CheckBox) findViewById(R.id.CheckBox3);
m_CheckBox4 = (CheckBox) findViewById(R.id.CheckBox4);
//对每个选项设置事件监听
m_CheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if (m_CheckBox1.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox1.getText());
}
}
});
m_CheckBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if (m_CheckBox2.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox2.getText());
}
}
});
m_CheckBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if (m_CheckBox3.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox3.getText());
}
}
});
m_CheckBox4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if (m_CheckBox4.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox4.getText());
}
}
});
//对按钮设置监听
m_Button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
int num = 0;
if (m_CheckBox1.isChecked()){
num++;
}
if (m_CheckBox2.isChecked()){
num++;
}
if (m_CheckBox3.isChecked()){
num++;
}
if (m_CheckBox4.isChecked()){
num++;
}
DisplayToast("谢谢参与!你一共选择了"+num+"项!");
}
});
}
//显示Toast
public void DisplayToast(String str){
Toast toast = Toast.makeText(this,str,Toast.LENGTH_SHORT);
//设置toast显示的位置
toast.setGravity(Gravity.TOP,0,220);
//显示该Toast
toast.show();
}
}
3-2:first/app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<CheckBox
android:id="@+id/CheckBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox1">
</CheckBox>
<CheckBox
android:id="@+id/CheckBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox2">
</CheckBox>
<CheckBox
android:id="@+id/CheckBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox3" >
</CheckBox>
<CheckBox
android:id="@+id/CheckBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox4" >
</CheckBox>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
3-3 : first/app/src/main/res/values/strings.xml
<resources>
<string name="hello">测验:Anddroid应用程序由以下哪些模块构成?</string>
<string name="app_name">firstapp</string>
<string name="CheckBox1">Activity</string>
<string name="CheckBox2">Intent</string>
<string name="CheckBox3">Content Provider</string>
<string name="CheckBox4">Service</string>
</resources>
五、实验结果

网友评论