牛骨文教育服务平台(让学习变的简单)

单选按钮(RadioButton)和复选框(CheckBox),状态开关按钮(ToggleButton),开关(Switch)都是普通的UI组件,都继承了Button类,因此都可以用Button的各种属性和方法。

RadioButton通常要与RadioGroup一起使用,用于定义一组单选按钮

对于二者而言,最主要的还是要看他们的监听器,RadioButton的事件监听器是:

单选按钮的监听接口是OnCheckedChangeListener

radiobutton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
 //单选框的监听器,OnCheckedChangeListener是单选框的监听接口,为radiogroup组建的Oncheck事件绑定监听器
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			  //处理操作
		
		}    	   
       });

而复选框CheckBox的监听器是:注意复选框的监听接口是CompoundButton.OnCheckedChangeListener

  //复选框的监听器,CompoundButton.OnCheckedChangeListener是复选框的监听接口
       checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
       
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			//处理操作
						
		}
		
       });
       

例:
main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性   别:"
            android:textSize="23dp" />
   <!-- RadioButton需要在RadioGroup组件里面 -->
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="男"
                android:textSize="23dp" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               
                android:text="女"
                android:textSize="23dp" />

        </RadioGroup>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="喜欢的颜色:"
            android:textSize="23dp" />
       <!-- 线性布局组件中有三个复选框 -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="红色" 
                android:textSize="23dp"/>

            <CheckBox
                android:id="@+id/checkBox2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="蓝色" 
                android:textSize="23dp"/>

            <CheckBox
                android:id="@+id/checkBox3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="绿色" 
                android:textSize="23dp"/>

        </LinearLayout>

    </TableRow>
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="23dp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp" />

</TableLayout>

MainActivity.java

package com.hust.radiocheckbuttontest;

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.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	 String str=null;
	 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //从布局文件中获取radiogroup组件和textview组件
       RadioGroup rg=(RadioGroup) findViewById(R.id.radioGroup1);
       final TextView showradio=(TextView) findViewById(R.id.textView3);
       final TextView showcheck=(TextView) findViewById(R.id.textView4);
       
       //获取复选框组件
       final CheckBox red=(CheckBox) findViewById(R.id.checkBox1);
       final CheckBox blue=(CheckBox) findViewById(R.id.checkBox2);
       final CheckBox green=(CheckBox) findViewById(R.id.checkBox3);
       //str.append("喜欢的颜色是:");
       //单选框的监听器,OnCheckedChangeListener是单选框的监听接口,为radiogroup组建的Oncheck事件绑定监听器
       rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			  //根据备选按钮的Id改变s的值
			String s=checkedId==R.id.radio0?"您的性别是男人":"您的性别是女人";
			//文本框显示文本
			showradio.setText(s);
		}    	   
       });
       //复选框的监听器,CompoundButton.OnCheckedChangeListener是复选框的监听接口
       red.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
       
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			if(isChecked){			    
				str=(String) red.getText();
				showcheck.setText(str);
			}
						
		}
		
       });
       
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}