轉載自:https://www.cnblogs.com/Im-Victor/p/6238437.html
僅僅是爲了以後查找方便,又怕博主刪掉原文。望見諒。
實現RadioButton由兩部分組成,也就是RadioButton和RadioGroup配合使用.RadioGroup是單選組合框,可以容納多個RadioButton的容器.在沒有RadioGroup的情況下,RadioButton可以全部都選中;當多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個。並用setOnCheckedChangeListener來對單選按鈕進行監聽
RadioGroup相關屬性:
RadioGroup.getCheckedRadioButtonId ();--獲取選中按鈕的id
RadioGroup.clearCheck ();//---清除選中狀態
RadioGroup.check (int id);//---通過參入選項id來設置該選項爲選中狀態如果傳遞-1作爲指定的選擇標識符來清除單選按鈕組的勾選狀態,相當於調用clearCheck()操作
setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener); //--一個當該單選按鈕組中的單選按鈕勾選狀態發生改變時所要調用的回調函數
addView (View child, int index, ViewGroup.LayoutParams params);//---使用指定的佈局參數添加一個子視圖
//參數 child 所要添加的子視圖 index 將要添加子視圖的位置 params 所要添加的子視圖的佈局參數
RadioButton.getText();//獲取單選框的值
//此外,RadioButton的checked屬性設置爲true,代碼裏調用RadioButton的check(id)方法,不會觸發onCheckedChanged事件
案例:
1.定義佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" >
<TextView
android:id="@+id/radiogroup_info_id"
android:layout_width="228px"
android:layout_height="wrap_content"
android:text="我選擇的是...?"
android:textSize="30sp"
/>
<RadioGroup
android:id="@+id/radioGroup_sex_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RadioButton
android:id="@+id/boy_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Boy"
/>
<RadioButton
android:id="@+id/girl_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Girl"
/>
</RadioGroup>
<Button
android:id="@+id/radio_clear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="清除選中按鈕"
/>
<Button
android:id="@+id/radio_add_child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="添加單選項"
/>
</LinearLayout>
</ScrollView>
2.Java代碼文件
package com.dream.app.start.first.radiobutton;
import com.dream.app.start.R;
import com.dream.app.start.R.id;
import com.dream.app.start.R.layout;
import com.dream.app.start.three.utils.PublicClass;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;
public class RadioButtonDemo extends PublicClass {
private TextView textView=null;
private RadioGroup radioGroup=null;
private RadioButton radioButton_boy,radioButton_girl;
private Button radio_clear,child;
/* (non-Javadoc)
* <a href="http://my.oschina.net/u/244147" target="_blank" rel="nofollow">@see</a>
* android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_frist_radiobuton);
textView = (TextView)findViewById(R.id.radiogroup_info_id);
//radioGroup
radioGroup=(RadioGroup)findViewById(R.id.radioGroup_sex_id);
radioButton_boy=(RadioButton)findViewById(R.id.boy_id);
radioButton_girl=(RadioButton)findViewById(R.id.girl_id);
child=(Button)findViewById(R.id.radio_add_child);
//---
radioGroup.setOnCheckedChangeListener(listen);
radio_clear=(Button)findViewById(R.id.radio_clear);
radio_clear.setOnClickListener(onClick);
child.setOnClickListener(onClick);
}
private OnCheckedChangeListener listen=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int id= group.getCheckedRadioButtonId();
switch (group.getCheckedRadioButtonId()) {
case R.id.girl_id:
textView.setText("我選擇的是:"+radioButton_girl.getText());
break;
case R.id.boy_id:
textView.setText("我選擇的是:"+radioButton_boy.getText());
break;
default:
textView.setText("我選擇的是:新增");
break;
}
}
};
private OnClickListener onClick=new OnClickListener() {
@Override
public void onClick(View v) {
radio_clear=(Button)v;
switch (radio_clear.getId()) {
case R.id.radio_clear:
radioGroup.check(-1);//清除選項
// radioGroup.clearCheck(); //清除選項
textView.setText("我選擇的是...?");
break;
case R.id.radio_add_child:
//新增子
RadioButton newRadio =new RadioButton(getApplicationContext());
newRadio.setText("新增");
radioGroup.addView(newRadio, radioGroup.getChildCount());
break;
//
default:
break;
}
}
};
}
運行示範 如圖:
RadioButton和RadioGroup的關係:
1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器
2、每個RadioGroup中的RadioButton同時只能有一個被選中
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中
4、大部分場合下,一個RadioGroup中至少有2個RadioButton
5、大部分場合下,一個RadioGroup中的RadioButton默認會有一個被選中,並建議您將它放在RadioGroup中的起始位置
RadioButton和CheckBox的區別:
1、單個RadioButton在選中後,通過點擊無法變爲未選中
單個CheckBox在選中後,通過點擊可以變爲未選中
2、一組RadioButton,只能同時選中一個
一組CheckBox,能同時選中多個
3、RadioButton在大部分UI框架中默認都以圓形表示
CheckBox在大部分UI框架中默認都以矩形表示
☆定製RadioButton樣式
RadioButton長成什麼樣子是由其Background、Button等屬性決定的,Android系統
使用style定義了默認的屬性,在android源碼
android/frameworks/base/core/res/res/values/styles.xml中可以看到默認的定義:
1 <style name="Widget.CompoundButton.RadioButton">
2 <item name="android:background">@android:drawable/btn_radio_label_background</item>
3 <item name="android:button">@android:drawable/btn_radio</item>
4 </style>
即其背景圖是btn_radio_label_background,其button的樣子是btn_radio
btn_radio_label_background是什麼?
其路徑是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_label_background.9.png
可以看到是一個NinePatch圖片,用來做背景,可以拉伸填充。
btn_radio是什麼?
其路徑是android/frameworks/base/core/res/res/drawable/btn_radio.xml
是個xml定義的drawable,打開看其內容:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/btn_radio_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/btn_radio_off" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/btn_radio_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/btn_radio_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/btn_radio_on_selected" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/btn_radio_off_selected" />
<item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>
自定義有三種方式:
1.方式一:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 未選中->
<item
android:state_checked="false"
android:drawable="@drawable/tabswitcher_long" />
<!--選中->
<item
android:state_checked="true"
android:drawable="@drawable/tabswitcher_short" />
</selector>
在佈局文件中使用
<RadioGroup
...
>
<RadioButton
...
android:button="@null"
android:background="@drawable/radio"
/>
</RadioGroup>
android:button="@null" 去除RadioButton前面的圓點
2.方式二:在JAVA代碼中定義
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getActionMasked() == MotionEvent.ACTION_DOWN){
this.setBackgroundResource(com.wxg.tab.R.drawable.main_bg);
}else if(event.getActionMasked()== MotionEvent.ACTION_DOWN) {
this.setBackgroundResource(com.wxg.tab.R.drawable.hui);
}
return super.onTouchEvent(event);
}
去除RadioButton前面的圓點adioButton.setButtonDrawable(android.R.color.transparent);
- 方式三
使用XML文件定義,在JAVA代碼中使用 radioButton.setBackgroundResource(R.drawable.radio);調用
==============================================================
設置RadioButton在文字的右邊
<b><RadioButton
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio" //在右邊
android:paddingLeft="30dip"
android:text="Android高手"
android:textSize="20dip" />
動態添加radiobutton進入radiogroup中。
RadioGroup radioGroup=findViewById(R.id.activity_group);
radioGroup.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 10; i++) {
RadioButton radioButton=new RadioButton(this);
radioButton.setText(""+i); //radiobutton的文字
radioButton.setTextSize(12);
radioButton.setTextColor(getResources().getColor(R.color.blue));
radioButton.setCompoundDrawablePadding(40); //貌似沒什麼用
radioButton.setPadding(20,0,20,0);//radiobutton之間的間距
radioButton.setCompoundDrawablePadding(20);
radioGroup.addView(radioButton);
}
如果一行的radiobutton過多,需要左右滑動,則佈局文件中可以在radiogroup外層嵌套一個scrollview
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/activity_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</HorizontalScrollView>