【1】RadioButton搭配RadioGroup使用html
【1】簡單的男女選擇Demo 這裏是本身寫死了就只有男、女的2種選擇 <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="性別" > <RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" > </RadioButton> <RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" > </RadioButton> </RadioGroup> 【2】 稍稍複雜一點的,只是寫一個容器,單個的RadioGroup的並不會在佈局文件中寫 <RadioGroup android:id="@+id/time_search" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > </RadioGroup>
Java部分代碼、 // 時間列表 private RadioGroup indicator; for (int i = 0; i < 7; i++) { //Java代碼中自定義RadioButton的UI式樣 RadioButton btn = new RadioButton(mContext); //設置觸摸效果 btn.setBackgroundResource(R.drawable.week_item_bg); btn.setButtonDrawable(new BitmapDrawable()); btn.setText("......"); Resources resource = getBaseContext().getResources(); //對於不一樣的按鈕狀態,採用不一樣的顏色顯示文字:能夠學習 ColorStateList csl = (ColorStateList) resource .getColorStateList(R.color.week_item_textcolor); if (csl != null) { btn.setTextColor(csl); } //設置監聽事件 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { } }); //在Java代碼中設置RadioGroup的一些屬性:須要學習一下LayoutParams的使用方法 RadioGroup.LayoutParams params = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); params.weight = 1; params.topMargin = 2; params.bottomMargin = 2; //繼續設置單個RadioButton的屬性 btn.setGravity(Gravity.CENTER); btn.setLayoutParams(params); //將定義好的RadioButton添加到RadioGroup indicator.addView(btn);
參考網址
java
http://blog.csdn.net/ganlijianstyle/article/details/7593812android
【2】經常使用方法屬性ide
1)android:checked=「」函數
2)監聽方法:佈局
setOnCheckedChangeListener()
3)得到checked項目:學習
getCheckedRadioButtonId()
4)demothis
RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup); //綁定一個匿名監聽器 group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { // TODO Auto-generated method stub //獲取變動後的選中項的ID int radioButtonId = arg0.getCheckedRadioButtonId(); //根據ID獲取RadioButton的實例 RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId); //更新文本內容,以符合選中項 tv.setText("您的性別是:" + rb.getText()); } });
5)補充.net
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事件