Android开发之RadioButton和CheckBox


RadioButton

 

类结构图:

 

RadioButton很好理解就是我们做Web开发里的单选按钮,需要注意的是我们在做Web开发需要将单选按钮的名字设置成一样,在这里Android开发,我们需要将两个或者更多的RadioButton放在一个RadioGroup里

实战演练:

1、如何设置RadioButton的对齐方式:

通过android:orientation其值有:vertical、horizontal

垂直:

  1. <RadioGroup android:id="@+id/raGroup01"  
  2.   
  3.       android:layout_width="fill_parent" android:layout_height="wrap_content"  
  4.   
  5.       android:orientation="vertical">  
  6.   
  7.       <RadioButton android:id="@+id/raBtn01" android:text="男" />  
  8.   
  9.       <RadioButton android:id="@+id/raBtn02" android:text="女" />  
  10.   
  11.    </RadioGroup>  

效果:

水平:

  1. <RadioGroup android:id="@+id/raGroup02"  
  2.   
  3.       android:layout_width="fill_parent" android:layout_height="wrap_content"  
  4.   
  5.       android:orientation="horizontal">  
  6.   
  7.       <RadioButton android:id="@+id/rautf" android:text="UTF" />  
  8.   
  9.       <RadioButton android:id="@+id/ragbk" android:text="GBK" />  
  10.   
  11.    </RadioGroup>  

效果:

2、如何让RadioButton设置成选中

android:checkedButton="@+id/rautf"

  1. <RadioGroup android:id="@+id/raGroup02"  
  2.   
  3.       android:layout_width="fill_parent" android:layout_height="wrap_content"  
  4.   
  5.       android:orientation="horizontal"  
  6.   
  7.       android:checkedButton="@+id/rautf">  
  8.   
  9.       <RadioButton android:id="@+id/rautf" android:text="UTF" />  
  10.   
  11.       <RadioButton android:id="@+id/ragbk" android:text="GBK" />  
  12.   
  13.    </RadioGroup>  

效果:

 

添加监听器:

  1. radioButton01 = (RadioButton) findViewById(R.id.raBtn01);  
  2.   
  3.    
  4.   
  5.       radioButton01.setOnClickListener(new OnClickListener() {  
  6.   
  7.         public void onClick(View v) {  
  8.   
  9.            String text = radioButton01.getText().toString();  
  10.   
  11.            System.out.println(text);  
  12.   
  13.    
  14.   
  15.         }  
  16.   
  17.       });  
  18.   
  19.    
  • 1
  • 2
  • 下一页

相关内容