首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 单选(RadioGroup )复选(CheckBox)旋钮的应用

2012-07-15 
android 单选(RadioGroup )复选(CheckBox)按钮的应用第一步,还是创建android项目。第二步,修改已经生成的re

android 单选(RadioGroup )复选(CheckBox)按钮的应用

第一步,还是创建android项目。

第二步,修改已经生成的res/layout/main.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"??
  • >??
  • ????<CheckBox????
  • ????????android:id="@+id/checkBox"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="复选框1"?/>??
  • ????<CheckBox????
  • ????????android:id="@+id/checkBox1"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="复选框2"?/>??
  • ????<RadioGroup???
  • ????????android:id="@+id/radioGroup"??
  • ????????android:orientation="vertical"??
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="fill_parent">??
  • ????????<RadioButton?android:id="@+id/radion1"??
  • ????????????android:layout_width="wrap_content"?android:layout_height="wrap_content"??
  • ????????????android:text="北京"?/>??
  • ????????<RadioButton?android:id="@+id/radion2"??
  • ????????????android:layout_width="wrap_content"?android:layout_height="wrap_content"??
  • ????????????android:text="天津"?/>??
  • ????????<RadioButton?android:id="@+id/radion3"??
  • ????????????android:layout_width="wrap_content"?android:layout_height="wrap_content"??
  • ????????????android:text="上海"?/>??
  • ????????<RadioButton?android:id="@+id/radion4"??
  • ????????????android:layout_width="wrap_content"?android:layout_height="wrap_content"??
  • ????????????android:text="重庆"?/>??
  • ????</RadioGroup>??
  • </LinearLayout>??

    ?

    第三步,添加主要代码

    ?

  • import?android.app.Activity;??
  • import?android.os.Bundle;??
  • import?android.widget.CheckBox;??
  • import?android.widget.CompoundButton;??
  • import?android.widget.RadioButton;??
  • import?android.widget.RadioGroup;??
  • import?android.widget.CompoundButton.OnCheckedChangeListener;??
  • ??
  • ??
  • public?class?checkBox?extends?Activity?implements?OnCheckedChangeListener??{??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ??????
  • ??????
  • ????RadioButton??r1?=?null;??
  • ????RadioButton??r2?=?null;??
  • ????RadioButton??r3?=?null;??
  • ????RadioButton??r4?=?null;??
  • ??????
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ??????????
  • ????????CheckBox?checkBox?=?(CheckBox)?findViewById(R.id.checkBox);???
  • ????????CheckBox?checkBox1?=?(CheckBox)?findViewById(R.id.checkBox1);??
  • ????????//获得单选按钮组??
  • ????????RadioGroup?radioGroup?=?(RadioGroup)findViewById(R.id.radioGroup);??
  • ????????//获得单选按钮??
  • ????????r1?=?(RadioButton?)findViewById(R.id.radion1);??
  • ????????r2?=?(RadioButton?)findViewById(R.id.radion2);??
  • ????????r3?=?(RadioButton?)findViewById(R.id.radion3);??
  • ????????r4?=?(RadioButton?)findViewById(R.id.radion4);??
  • ??????????
  • ????????checkBox.setChecked(true);???
  • ??????????
  • ????????r1.setClickable(true);??
  • ????????//监听多选按钮??
  • ????????checkBox.setOnCheckedChangeListener(this);??
  • ????????checkBox1.setOnCheckedChangeListener(this);??
  • ????????//监听单选按钮??
  • ????????radioGroup.setOnCheckedChangeListener(mChangeRadio);??
  • ??????????
  • ????} ?
  • //参考

  • ??http://blog.csdn.net/applezhengxd/archive/2010/05/06/5563512.aspx

  • 热点排行