1. main.xml 布局文件

<?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" >        <RadioGroup         android:id="@+id/groupId"        android:orientation="vertical"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        >        <RadioButton             android:id="@+id/male"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/male"            android:checked="true"                        />        <RadioButton             android:id="@+id/female"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/female"            />            </RadioGroup>        <TextView         android:id="@+id/love"        android:text="@string/love"        android:layout_width="wrap_content"        android:layout_height="wrap_content"                />        <CheckBox         android:id="@+id/swinning"        android:text="@string/swinning"        android:layout_width="wrap_content"        android:layout_height="wrap_content">    </CheckBox>        <CheckBox         android:id="@+id/running"        android:text="@string/running"        android:layout_width="wrap_content"        android:layout_height="wrap_content">    </CheckBox>        <CheckBox         android:id="@+id/reading"        android:text="@string/reading"        android:layout_width="wrap_content"        android:layout_height="wrap_content">    </CheckBox></LinearLayout>

2. Java 调用

package com.helloworld;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast;/** * 常控件的使用 * @author liuqing * @version 1.0 * */public class ViewCompoentActivity extends Activity {private RadioGroup radioGroup;private CheckBox running;private CheckBox reading;private CheckBox swinning;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        this.radioGroup = (RadioGroup)this.findViewById(R.id.groupId);        this.reading = (CheckBox)this.findViewById(R.id.reading);        this.swinning = (CheckBox)this.findViewById(R.id.swinning);        this.running = (CheckBox)this.findViewById(R.id.running);        //对readioGroup 添加监听器        this.radioGroup.setOnCheckedChangeListener(        new OnCheckedChangeListener(){                    //这里是返加的ID值@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if (checkedId == R.id.female) {Toast.makeText(getApplication(), "female",Toast.LENGTH_SHORT).show();}else if (checkedId == R.id.male) {Toast.makeText(getApplication(), "male", Toast.LENGTH_SHORT).show();}}                }       );        this.running.setOnCheckedChangeListener               (new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {//如果被选中isChecked 值为trueif (isChecked) {System.out.println("running is true");Toast.makeText(getApplication(), "running is true", Toast.LENGTH_SHORT).show();}else {System.out.println("running is false");}}                     }            );                this.swinning.setOnCheckedChangeListener(        new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {System.out.println("swinning is true");Toast.makeText(getApplication(), "swinning is true", Toast.LENGTH_SHORT).show();}else {System.out.println("swinning is false");}}                          }        );                this.reading.setOnCheckedChangeListener(        new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {System.out.println("reading is true");Toast.makeText(getApplication(), "reading is true",Toast.LENGTH_SHORT).show();}else {System.out.println("reading is false");}}                       }        );            }}

更多相关文章

  1. Android控件笔记——在界面中显示图片
  2. Android 获取控件宽高的3种方法
  3. Android UI控件详解-CheckBox(多选框)
  4. Android UI控件详解-Gallery(画廊)
  5. Android UI控件详解-ImageSwitcher(图片切换器)
  6. Android 滑动开关控件
  7. Android 学习之- 单选按钮、复选框、状态开关、时钟控件
  8. Android 控件之八:AutoCompleteTextView自动完成文本框
  9. Android UI控件详解-RadioGroup和RadioButton(单选框)

随机推荐

  1. Android(安卓)NDK区分第一次起机-sqlite3
  2. 浅谈android的selector背景选择器
  3. Android(安卓)开发艺术探索笔记之二 -- I
  4. Android实现导航菜单左右滑动效果
  5. Android(安卓)蓝牙模块
  6. Android(安卓)socket 开发中遇到的问题.
  7. 用CSS3生成的一个漂亮的android客户端页
  8. Android下Affinities和Task
  9. Android(安卓)Push Notification实现信息
  10. Android(安卓)AES加密算法及事实上现