文章1 轉自:http://my.oschina.net/amigos/blog/59261
實現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、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器
2、每個RadioGroup中的RadioButton同時只能有一個被選中
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中
4、大部分場合下,一個RadioGroup中至少有2個RadioButton
5、大部分場合下,一個RadioGroup中的RadioButton默認會有一個被選中,並建議您將它放在RadioGroup中的起始位置
看案例:
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" rel=nofollow target=_blank>@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; } } }; }
運行效果:
3.
4:可以通過設置如下屬性可以使單選按鈕在顯示文本的右邊
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
效果:
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中可以看到默認的定義:
<style name="Widget.CompoundButton.RadioButton"> <item name="android:background">@android:drawable/btn_radio_label_background</item> <item name="android:button">@android:drawable/btn_radio</item> </style>
其路徑是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>
意思即為當radiobutton被選中時,並且被按下時,其Button應該長成btn_radio_on_pressed這個樣子。
文件是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_on_pressed.png
drawable的item中可以有以下屬性:
android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_active=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"]
從上面分析我們如果要修改RadioButton的外觀,
自定義有三種方式:
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>
2.方式二:在JAVA代碼中定義
<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" /> </B>
3. 方式三
使用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" /> </B>
自定義 radiobutton 文字顏色隨選中狀態而改變
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="@color/color_text_selected"/> <!-- not selected --> <item android:color="@color/color_text_normal"/> </selector>
使用XML文件很簡單就可以實現,但是有時必須要使用java code 的方式動態實現,這就有些復雜了,這需要繼承RadioButton並覆蓋其中的onDraw方法。
在代碼中的image是Bitmap對象。
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(image!=null){ Paint pt = new Paint(); pt.setARGB(255,66,66,66); //消除鋸齒 pt.setAntiAlias(true); //居中顯示圖片 int imageX=(int)(this.getWidth()-image.getWidth())/2; canvas.drawBitmap(image,imageX,5,pt); pt.setARGB(255,255,255,255); //居中顯示字符串 int strX=(int)(this.getWidth()-name.getBytes().length*5.5)/2; canvas.drawText(name,strX,(image.getHeight()+15),pt); } }
另一篇:http://www.android-study.com/jichuzhishi/369.html
Android RadioGroup有時候很有用,給用戶提供多選一機制,現在要繪制一個畫面,事例如下:
上面是一個TextView,下面有個RadioGroup,布局如下:
主布局 main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!--第一個TextView --> <TextView android:id="@+id/myTextView" android:layout_width="228px" android:layout_height="49px" android:text="@string/str_radio_question1" android:textSize="30sp" /> <!--建立一個RadioGroup --> <RadioGroup android:id="@+id/myRadioGroup" android:layout_width="137px" android:layout_height="216px" android:orientation="vertical"> <!--第一個RadioButton --> <RadioButton android:id="@+id/myRadioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tr_radio_op1" /> <!--第二個RadioButton --> <RadioButton android:id="@+id/myRadioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tr_radio_op2" /> </RadioGroup> </LinearLayout>
package com.android.test; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class RadioGroupDemo extends Activity { public TextView mTextView1; public RadioGroup mRadioGroup1; public RadioButton mRadio1, mRadio2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 取得 TextView、RadioGroup、RadioButton對象 mTextView1 = (TextView) findViewById(R.id.myTextView); mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup); mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1); mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2); // RadioGroup用OnCheckedChangeListener來運行 mRadioGroup1.setOnCheckedChangeListener(mChangeRadio); } private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if (checkedId == mRadio1.getId()) { // 把mRadio1的內容傳到mTextView1 mTextView1.setText(mRadio1.getText()); } else if (checkedId == mRadio2.getId()) { // 把mRadio2的內容傳到mTextView1 mTextView1.setText(mRadio2.getText()); } } }; }
需要注意的就是RadioGroup的消息處理。
mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);