Android RadioGroup和RadioButton案例及詳解

來源:互聯網
上載者:User

實現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事件

 

RadioButton和RadioGroup的關係:

1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器

2、每個RadioGroup中的RadioButton同時只能有一個被選中

3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中

4、大部分場合下,一個RadioGroup中至少有2個RadioButton

5、大部分場合下,一個RadioGroup中的RadioButton預設會有一個被選中,並建議您將它放在RadioGroup中的起始位置

看案列:http://my.oschina.net/amigos/blog/59261
              

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.