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. 如果Imageview与Linearlayout有叠加且可选资源长度不同,如何布局?
  2. Android控件笔记——在界面中显示图片
  3. Android(安卓)获取控件宽高的3种方法
  4. Android(安卓)UI控件详解-CheckBox(多选框)
  5. Android(安卓)UI控件详解-Gallery(画廊)
  6. Android(安卓)UI控件详解-ImageSwitcher(图片切换器)
  7. Android(安卓)滑动开关控件
  8. Android(安卓)学习之- 单选按钮、复选框、状态开关、时钟控件
  9. Android(安卓)控件之八:AutoCompleteTextView自动完成文本框

随机推荐

  1. android studio jks和eclipse keystore
  2. Android系统根目录结构及说明
  3. Android内存监测工具DDMS-->Heap
  4. 5 个顶级 Android 开源库
  5. Android(安卓)常用设计模式
  6. Android(安卓)中的防锯齿
  7. Android在XML中自定义控件的使用
  8. Android 7+的一些报错
  9. Android学习笔记SQLite
  10. 为什么比起 Metro 我更喜欢 Holo