UI组件之TextView及其子类(二)RadioButton和CheckBox

单选按钮(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




    

        
   
        

            

            

        

    

    

        
       
        

            

            

            

        

    
    

    


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);
    }
}
UI组件之TextView及其子类(二)RadioButton和CheckBox_第1张图片




你可能感兴趣的:(【Android,知识框架】,Android学习笔记)