checkbox就是选择框。
效果:
layout中的xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/textview1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="选择性别:"
- />
- <CheckBox
- android:id="@+id/Checkbox1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="男"
- />
- <CheckBox
- android:id="@+id/Checkbox2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="女"
- />
- //
- <RadioGroup
- android:id="@+id/myRadioG"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <RadioButton
- android:id="@+id/myRadio1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="篮球"/>
- <RadioButton
- android:id="@+id/myRadio2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="足球"/>
- <RadioButton
- android:id="@+id/myRadio3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="羽毛球"/>
- </RadioGroup>
- </LinearLayout>
java文件:
- package com.cheng.checkbox;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- public class CheckBoxActivity extends Activity {
- private CheckBox mCheckBox1;
- private CheckBox mCheckBox2;
- private RadioGroup mRadioGroup;
- private RadioButton mRadioButton1;
- private RadioButton mRadioButton2;
- private RadioButton mRadioButton3;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.main);
- mCheckBox1 = (CheckBox)findViewById(R.id.Checkbox1);
- mCheckBox2 = (CheckBox)findViewById(R.id.Checkbox2);
- mRadioGroup = (RadioGroup)findViewById(R.id.myRadioG);
- mRadioButton1 = (RadioButton)findViewById(R.id.myRadio1);
- mRadioButton2 = (RadioButton)findViewById(R.id.myRadio2);
- mRadioButton3 = (RadioButton)findViewById(R.id.myRadio3);
- OnClickListener ol = new OnClickListener() {
- //设置监听
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.Checkbox1:
- //将选中的Radio和Checkbox的内容用Toast显示出来,第一个参数是用来显示的组件,第二个参数是要显示的内容,第三个参数是显示的时间
- Toast.makeText(CheckBoxActivity.this, "choose "+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();
- break;
- case R.id.Checkbox2:
- Toast.makeText(CheckBoxActivity.this, "choose"+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();
- break;
- case R.id.myRadio1:
- Toast.makeText(CheckBoxActivity.this, "choose"+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();
- break;
- case R.id.myRadio2:
- Toast.makeText(CheckBoxActivity.this, "choose"+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();
- break;
- case R.id.myRadio3:
- Toast.makeText(CheckBoxActivity.this, "choose"+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();
- break;
- default:
- break;
- }
- }
- };
- //设置监听,当checkbox和RadioButton的选择发生改变的时候发生
- OnCheckedChangeListener occl = new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked)
- {
- Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_LONG).show();
- }
- }
- };
- //将监听事件加到RadioButton和CheckBox组件上
- mCheckBox1.setOnClickListener(ol);
- mCheckBox2.setOnClickListener(ol);
- mCheckBox1.setOnCheckedChangeListener(occl);
- mCheckBox2.setOnCheckedChangeListener(occl);
- mRadioButton1.setOnCheckedChangeListener(occl);
- mRadioButton2.setOnCheckedChangeListener(occl);
- mRadioButton3.setOnCheckedChangeListener(occl);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
转载于:https://blog.51cto.com/jackcheng/1124317