该工程的功能实现在一个activity中显示一个单选框和一个多选框
以下代码是MainActivity.java文件中的代码
package com.example.checkbox; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends Activity { //对控件对象进行声明 private RadioGroup gendergroup = null; private RadioButton femaleButton = null; private RadioButton maleButton = null; private CheckBox swimBox = null; private CheckBox runBox = null; private CheckBox readBox = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //通过控件的ID来得到代表控件的对象 gendergroup = (RadioGroup)findViewById(R.id.genderGroup); femaleButton = (RadioButton)findViewById(R.id.femaleButton); maleButton = (RadioButton)findViewById(R.id.maleButton); swimBox = (CheckBox)findViewById(R.id.swim); runBox = (CheckBox)findViewById(R.id.run); readBox = (CheckBox)findViewById(R.id.read); //设置监听器 gendergroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(femaleButton.getId() == checkedId){ System.out.println("female"); Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show(); } else if(maleButton.getId() == checkedId) { System.out.println("male"); Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show(); } } }); //为多选按钮添加监听器 swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("swim is checked"); } else { System.out.println("swim is unchecked"); } } }); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("run is checked"); } else { System.out.println("run is unchecked"); } } }); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("read is checked"); } else { System.out.println("read is unchecked"); } } }); } }
以下代码是activity_main.xml文件中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <RadioGroup android:id="@+id/genderGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /> </RadioGroup> <CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /> <CheckBox android:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /> <CheckBox android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" /> </LinearLayout>
以下代码是string.xml文件中的代码
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">CheckBox</string> <string name="hello_world">Hello world!</string> <string name="female">女</string> <string name="male">男</string> <string name="swim">游泳</string> <string name="run">跑步</string> <string name="read">读书</string> </resources>
时间: 2024-10-28 11:36:10