Android UI(RadioButton)详解

目录:
    1.应用场景      
    2.RadioButton一些重要属性       
    3.RadioButton简单使用    
    4.动态添加RadioButton选项    
    5.RadioButton自定义

1.应用场景
    应用于在多项中只能选取单项的情况,例如性别,但必须与RadioGroup结合使用,如果单独使用的话无法达到单选的结果
    
2.RadioButton一些重要属性 
    1.RadioGroup常用属性
        指定选项横向排列:android:orientation="horizontal"
        指定选项纵向排列:android:orientation="vertical"
        
    2.设置RadioButton颜色(API level> = 21):android:buttonTint="@color/your_color"
    
    3.在自定义背景的时候设置android:button="@null",盖掉原背景
    
    4.设置文字在左,图片在右(下面属性一起设置)
      android:button="@null"
      android:drawableRight="@android:drawable/btn_radio"
                    
    ps:RadioButton与Checkbox的区别在于RadioButton选择后不能再点击取消选择,只能选择其他项,取消前一项的选择 

3.RadioButton简单使用

    1.xml布局



    
    
        
        


    


    
    2. Java实现代码
package com.andy.androiduiradiobutton;


import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.Toast;


public class MainActivity extends Activity {
	private RadioButton radioboy,radiogirl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//初始化
		radioboy = (RadioButton) findViewById(R.id.radio_boy);
		radiogirl = (RadioButton) findViewById(R.id.radio_girl);
		//radioboy设置监听
		radioboy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Toast.makeText(MainActivity.this, "男",Toast.LENGTH_SHORT).show();
				}
			}
		});
		//radiogirl设置监听
		radiogirl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Toast.makeText(MainActivity.this, "女",Toast.LENGTH_SHORT).show();
			}
		});
	}
}
   
    3.效果截图
Android UI(RadioButton)详解_第1张图片

    
4.动态添加RadioButton选项 
    1. 布局文件



    


    


   
    2. Java实现代码
package com.andy.androiduiradiobutton;


import android.accounts.Account;
import android.accounts.OnAccountsUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Toast;


public class MainActivity extends Activity {
	private RadioGroup mRadioGroup;
	private RelativeLayout mRelativeLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//初始化RadioGroup RelativeLayout
		mRadioGroup = (RadioGroup) findViewById(R.id.mRadioGroup);
		mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
		//先删除已经存在的RadioGroup,否则会报错:The specified child already has a parent.
		mRelativeLayout.removeView(mRadioGroup);
		//动态添加RadioButton
		for(int i=1;i<4;i++){
			RadioButton mRadioButton = new RadioButton(this);
			mRadioButton.setText("mRadioButton"+i);
			//选项添加到RadioGroup
			mRadioGroup.addView(mRadioButton);
		}
		
		//RadioGroup添加到布局
		mRelativeLayout.addView(mRadioGroup);
		//设置监听
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				RadioButton mRadioButton1 = (RadioButton) findViewById(checkedId);	
				Toast.makeText(MainActivity.this, mRadioButton1.getText(), Toast.LENGTH_SHORT).show();
	}
			});
	}
	
}
 

    3. 效果截图

Android UI(RadioButton)详解_第2张图片

5.RadioButton自定义

  1. 通过图片背景+selector自定义
(1)定义selector radio_buttonbg.xml

  
    
    
	 
    

(2)主布局文件



    
        
		     
        
       
    



 注:android:background="@drawable/radio_buttonbg"设置的图片在文本底部,android:drawablePadding="10dp"设置图片在文字的右边,android:button="@null" 隐藏原来的图标
 
 
(3)Java实现代码
package com.andy.androiduiradiobutton;


import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.Toast;


public class MainActivity extends Activity {
	private RadioButton radioboy,radiogirl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//初始化
		radioboy = (RadioButton) findViewById(R.id.radio_boy);
		radiogirl = (RadioButton) findViewById(R.id.radio_girl);
		//radioboy设置监听
		radioboy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Toast.makeText(MainActivity.this, "男",Toast.LENGTH_SHORT).show();
				}
			}
		});
		//radiogirl设置监听
		radiogirl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Toast.makeText(MainActivity.this, "女",Toast.LENGTH_SHORT).show();
			}
		});
	}
}

(4)效果截图

Android UI(RadioButton)详解_第3张图片

  2. 通过xml绘制+selector自定义
(1) radio_checked_shape.xml


  
	
	    
	    
	    
	    
	    
	      
   
  
	
	    
	    
	    
	    
	    
	    
	    
	        
       

(2) radio_unchecked_shape.xml  


    
    
    
    

(3) radio_selector_shape.xml


    
    
    
	

(4) 主布局xml



    
        
        
 
        
    



(5) Java实现代码
package com.andy.androiduiradiobutton;


import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.Toast;


public class MainActivity extends Activity {
	private RadioButton radioboy,radiogirl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//初始化
		radioboy = (RadioButton) findViewById(R.id.radio_boy);
		radiogirl = (RadioButton) findViewById(R.id.radio_girl);
		//radioboy设置监听
		radioboy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Toast.makeText(MainActivity.this, "男",Toast.LENGTH_SHORT).show();
				}
			}
		});
		//radiogirl设置监听
		radiogirl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Toast.makeText(MainActivity.this, "女",Toast.LENGTH_SHORT).show();
			}
		});
	}
}

6.效果截图

 Android UI(RadioButton)详解_第4张图片

  3. 通过改变@android:style/Widget.CompoundButton.RadioButton系统内置syle风格自定义


(1) RadioButton的样式就采用2中的xml radio_checked_shape.xml,radio_unchecked_shape.xml,radio_selector_shape.xml与2一致


(2)styles.xml



    
    


    
    
    
    
    


 
(3) 主布局文件



    
        
 
        
    



 (4)Java实现代码(与2一致)
 
 (5)效果截图
  Android UI(RadioButton)详解_第5张图片
 

你可能感兴趣的:(android)