RadioGroup實(shí)現(xiàn)單選框的多行排列
RadioGroup的使用非常簡(jiǎn)單,只是一般情況下,只能是橫向排列或豎向排列.如果讓多橫排列的的就不是那么簡(jiǎn)單的了。
也許有童鞋該說(shuō)了,將RadioButton寫(xiě)到LineLayout中不久行了嗎?經(jīng)過(guò)檢驗(yàn)確實(shí)可以那樣做,剛開(kāi)始我也是這樣做到.不過(guò)運(yùn)行起來(lái)發(fā)現(xiàn)了了一個(gè)bug---單選按鈕不在是單選了.而且選擇事件不會(huì)被監(jiān)聽(tīng)到.這就要求我們?nèi)ハ朕k法了.其實(shí)實(shí)現(xiàn)起來(lái)也不難.只要多用幾個(gè)RadioGroup就可以了(要在代碼中處理一些事件)。
上代碼:
1.xml中的布局:
<RelativeLayout
android:id="@+id/main_tab_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="30dp">
<RadioGroup
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="5dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="GBP英鎊" />
<RadioButton
android:id="@+id/rb_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="HKD港元" />
<RadioButton
android:id="@+id/rb_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="USD美元Ԫ" />
</RadioGroup>
<RadioGroup
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@+id/radio1"
android:layout_margin="5dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="CHF瑞士法郎" />
<RadioButton
android:id="@+id/rb_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="SGD新加坡元" />
<RadioButton
android:id="@+id/rb_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="SEK瑞典克朗" />
</RadioGroup>
<RadioGroup
android:id="@+id/radio3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@+id/radio2"
android:layout_margin="5dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="JPY日元" />
<RadioButton
android:id="@+id/rb_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="CAD加拿大元Ԫ" />
<RadioButton
android:id="@+id/rb_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/RB_text_size"
android:text="AUD澳大利亞元" />
</RadioGroup>
<RadioGroup
android:id="@+id/radio4"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@+id/radio3"
android:layout_margin="5dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/RB_text_size"
android:text="EOR歐元Ԫ" />
</RadioGroup>
</RelativeLayout>
這樣就實(shí)現(xiàn)了多行布局,這只是我布局中的一部分,其中 android:textSize=”@dimen/RB_text_size” 為自己定義的字體大?。?/p>
2.activity中的使用以及處理:
public class SelectMoneyActivity extends BaseActivity {
String strBtnSelected = ""; //記錄選擇的是哪個(gè)選項(xiàng)
private RadioGroup rg1, rg2, rg3, rg4;
private RadioButton rb_1, rb_2, rb_3, rb_4, rb_5, rb_6, rb_7, rb_8, rb_9, rb_10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_money);
initView();
}
private void initView() {
rg1 = (RadioGroup) findViewById(R.id.radio1);
rg2 = (RadioGroup) findViewById(R.id.radio2);
rg3 = (RadioGroup) findViewById(R.id.radio3);
rg4 = (RadioGroup) findViewById(R.id.radio4);
rb_1 = (RadioButton) findViewById(R.id.rb_1);
rb_2 = (RadioButton) findViewById(R.id.rb_2);
rb_3 = (RadioButton) findViewById(R.id.rb_3);
rb_4 = (RadioButton) findViewById(R.id.rb_4);
rb_5 = (RadioButton) findViewById(R.id.rb_5);
rb_6 = (RadioButton) findViewById(R.id.rb_6);
rb_7 = (RadioButton) findViewById(R.id.rb_7);
rb_8 = (RadioButton) findViewById(R.id.rb_8);
rb_9 = (RadioButton) findViewById(R.id.rb_9);
rb_10 = (RadioButton) findViewById(R.id.rb_10);
btn_back = (Button) findViewById(R.id.btn_back);
btn_next = (Button) findViewById(R.id.btn_next);
//創(chuàng)建監(jiān)聽(tīng)器,為每個(gè)RadioButton注冊(cè)監(jiān)聽(tīng)
BtnSelected btnSelected1 = new BtnSelected("1");
BtnSelected btnSelected2 = new BtnSelected("2");
BtnSelected btnSelected3 = new BtnSelected("3");
BtnSelected btnSelected4 = new BtnSelected("4");
BtnSelected btnSelected5 = new BtnSelected("5");
BtnSelected btnSelected6 = new BtnSelected("6");
BtnSelected btnSelected7 = new BtnSelected("7");
BtnSelected btnSelected8 = new BtnSelected("8");
BtnSelected btnSelected9 = new BtnSelected("9");
BtnSelected btnSelected10 = new BtnSelected("10");
rb_1.setOnClickListener(btnSelected1);
rb_2.setOnClickListener(btnSelected2);
rb_3.setOnClickListener(btnSelected3);
rb_4.setOnClickListener(btnSelected4);
rb_5.setOnClickListener(btnSelected5);
rb_6.setOnClickListener(btnSelected6);
rb_7.setOnClickListener(btnSelected7);
rb_8.setOnClickListener(btnSelected8);
rb_9.setOnClickListener(btnSelected9);
rb_10.setOnClickListener(btnSelected10);
//點(diǎn)擊事件的監(jiān)聽(tīng)器
public class BtnSelected implements View.OnClickListener {
private String btnId;
public BtnSelected(String str) {
btnId = str;
}
@Override
public void onClick(View v) {
strBtnSelected = btnId; //選擇的某一項(xiàng)
isSelect = true;
//點(diǎn)擊了第一行 ,就把另外行的點(diǎn)擊項(xiàng)清空
if (btnId.equals("1") || btnId.equals("2") || btnId.equals("3")) {
rg2.clearCheck();
rg3.clearCheck();
rg4.clearCheck();
} else if (btnId.equals("4") || btnId.equals("5") || btnId.equals("6")) {
rg1.clearCheck();
rg3.clearCheck();
rg4.clearCheck();
} else if (btnId.equals("7") || btnId.equals("8") || btnId.equals("9")) {
rg1.clearCheck();
rg2.clearCheck();
rg4.clearCheck();
} else {
rg1.clearCheck();
rg2.clearCheck();
rg3.clearCheck();
}
}
}
}
已經(jīng)搞定.還有一種方法就是自定義RadioGroup實(shí)現(xiàn),不過(guò)這種有點(diǎn)復(fù)雜.我還是下班回家了.
補(bǔ)充:
使用RadioGroup.setcheck(RadioButton的id)初始化默認(rèn)選中A按鈕,但是監(jiān)聽(tīng)不會(huì)執(zhí)行的問(wèn)題
解決:因?yàn)橐呀?jīng)給A按鈕在布局中設(shè)置了check=”true”; 將這個(gè)屬性去掉就會(huì)執(zhí)行監(jiān)聽(tīng)了.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android采用消息推送實(shí)現(xiàn)類(lèi)似微信視頻接聽(tīng)
欄 目:Android
下一篇:Android實(shí)現(xiàn)懸浮窗全系統(tǒng)版本
本文標(biāo)題:RadioGroup實(shí)現(xiàn)單選框的多行排列
本文地址:http://www.jygsgssxh.com/a1/Android/9081.html
您可能感興趣的文章
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局
- 01-10Android實(shí)現(xiàn)底部導(dǎo)航欄的主界面


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)


