Activity常用控件二——RadioGroup、RadioButton、CheckBox的使用

笔记摘要:

这里主要简单的介绍了RadioGroup中的RadioButton和CheckBox的创建方法和监听器的设置


需要注意的是:RadioGroup的监听器和Button控件的监听器有所不同,分别为:

RadioGroup.OnCheckedChangeListener

CompoundButton.OnCheckedChangeListener


示例图示:




radio.xml代码:采用RelativeLayout的布局



    



    

        
   
        
         
        

    
    
    
   
    
    

    


RadioActivityjava代码

package com.example.ratio;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.CompoundButton;

public class RatioActivity extends Activity {

	private final String TAG = "RatioActivity" ;
	private RadioGroup radioGroup = null;
	private RadioButton femaleButton = null;
	private RadioButton maleButton = null;
	private CheckBox swimBox = null;
	private CheckBox footballBox = null;
	private CheckBox basketballBox = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ratio);
        //获取各种组件对象
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        femaleButton = (RadioButton) findViewById(R.id.female);
        maleButton = (RadioButton) findViewById(R.id.male);
        swimBox= (CheckBox) findViewById(R.id.swim);
        footballBox= (CheckBox) findViewById(R.id.football);
        basketballBox= (CheckBox) findViewById(R.id.basketball);
        
        //为RadioGroup设置监听器,需要注意的是:这里的监听器和Button控件的监听器有所不同
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(femaleButton.getId()==checkedId){
					Log.i(TAG,"female");
					Toast.makeText(RatioActivity.this, "female", Toast.LENGTH_LONG).show();
				}
				else if(maleButton.getId()==checkedId){
					Log.i(TAG,"male");
					Toast.makeText(RatioActivity.this, "male", Toast.LENGTH_LONG).show();
				}
			}
		});
    

   //为多选按钮添加监听器
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        	
        	public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
        		if(isChecked){
        			Log.i(TAG,"swim is checked");
        		}
        		else{
        			Log.i(TAG,"swim is unchecked");
        		}
        		
        	}
        });
        
    basketballBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
    	public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
    	
    		if(isChecked){
    			Log.i(TAG,"basketball is checked");
    		}
    		else{
    			Log.i(TAG,"basketball is unchecked");
    		}
    		}
    	});
    
    footballBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		
		//@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			if(isChecked){
				Log.i(TAG,"football is checked");
			}
			else{
				Log.i(TAG,"football is unchecked");
			}
		}
	});
    
	 basketballBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			if(isChecked){
				Log.i(TAG,"basketball is checked");
			}
			else{
				Log.i(TAG,"basketball is unchecked");
			}
		}
	});}}
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
  

转载于:https://www.cnblogs.com/xushuai123/archive/2012/11/06/2978054.html

你可能感兴趣的:(Activity常用控件二——RadioGroup、RadioButton、CheckBox的使用)