標籤:android 控制項 基礎
RadioButton和RadioGroup的關係:
1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器;
2、每個RadioGroup中的RadioButton同時只能有一個被選中;
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中;
4、一個RadioGroup中至少有2個RadioButton;
5、大部分場合下,一個RadioGroup中的RadioButton預設會有一個被選中,並建議您將它放在RadioGroup中的起始位置(預設選中方法android:checked="true" );
我們在代碼中使用 setOnCheckedChangeListener 來對選項按鈕進行監聽。
程式碼範例:
XML檔案:
<span style="font-size:12px;"><?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 android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/radioBtn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radioBtn1" android:textColor="#ffffff" /> <RadioButton android:id="@+id/radioBtn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radioBtn2" /> </RadioGroup> </LinearLayout> </span>
布局格式
若是希望,文字在前單選框在後,只要在main.xml布局檔案中的<RadioButton/>加入
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"即可
效果及如顯示:
string.xml
<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">選項按鈕測試</string> <string name="hello">你的性別是</string> <string name="radioBtn1">男</string> <string name="radioBtn2">女</string></resources> </span>
MainActivity.java
<span style="font-size:12px;">package com.android.radiobutton; import android.app.Activity; import android.os.Bundle; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends Activity { //聲明RadioGroup RadioGroup raGroup; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通過findViewById獲得RadioGroup對象 raGroup=(RadioGroup)findViewById(R.id.radioGroup); // 添加事件監聽器 raGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(checkedId==R.id.radioBtn1){ Toast.makeText(MainActivity.this, "你的性別是男", Toast.LENGTH_LONG).show(); }</span><pre name="code" class="java"><span style="font-size:12px;"> else { Toast.makeText(MainActivity.this, "你的性別是女", Toast.LENGTH_LONG).show(); } } }); } } </span>
延伸:RadioButton和CheckBox的區別:
1、單個RadioButton在選中後,通過點擊無法變為未選中
單個CheckBox在選中後,通過點擊可以變為未選中
2、一組RadioButton,只能同時選中一個
一組CheckBox,能同時選中多個
3、RadioButton在大部分UI架構中預設都以圓形表示
CheckBox在大部分UI架構中預設都以矩形表示
具體程式碼範例:
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 android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="20sp" android:textStyle="bold" android:textColor="#FFFFFF" /> <CheckBox android:id="@+id/checkbox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/football" android:textSize="16sp" /> <CheckBox android:id="@+id/checkbox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/basketball" android:textSize="16sp" /> <CheckBox android:id="@+id/checkbox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/volleyball" android:textSize="16sp"/></LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">你喜歡的運動是</string> <string name="app_name">複選按鈕測試</string> <string name="football">足球</string> <string name="basketball">籃球</string> <string name="volleyball">排球</string></resources>
MainActivity.java
package com.android.checkbox; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast; import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends Activity{ //聲明複選按鈕 private CheckBox cBox1; private CheckBox cBox2; private CheckBox cBox3; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //通過findViewById獲得CheckBox對象 cBox1=(CheckBox)findViewById(R.id.checkbox1); cBox2=(CheckBox)findViewById(R.id.checkbox2); cBox3=(CheckBox)findViewById(R.id.checkbox3); //註冊事件監聽器 cBox1.setOnCheckedChangeListener(listener); cBox2.setOnCheckedChangeListener(listener); cBox3.setOnCheckedChangeListener(listener); } //響應事件 private OnCheckedChangeListener listener = new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //cBox1被選中 if (buttonView.getId()==R.id.checkbox1){ if (isChecked){ Toast.makeText(MainActivity.this, "你喜歡足球", Toast.LENGTH_LONG).show(); } } //cBox2被選中 else if (buttonView.getId()==R.id.checkbox2){ if (isChecked){ Toast.makeText(MainActivity.this, "你喜歡籃球", Toast.LENGTH_LONG).show(); } } //cBox3被選中 else if (buttonView.getId()==R.id.checkbox3){ if (isChecked){ Toast.makeText(MainActivity.this, "你喜歡排球", Toast.LENGTH_LONG).show(); } } } }; }
如下:
到此結束~~~
android 控制項之RadioGroup&RadioButton