一簡介:
RadioGroup作為一個單選按鈕組,可以設置為性別選擇男或則女,地址選擇等等,作為一個android入門級選手,就簡單的說一下RadioGroup組中RadioButton的布局和初始化操作,以及禁用整個RadioGroup。
二具體介紹:
布局:
<RadioGroup android:id="@+id/rg_Orientation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp"> <RadioButton android:id="@+id/rb_Portrait" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:drawableRight="@drawable/r_portrait" /> <RadioButton android:id="@+id/rb_Landscape" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@drawable/r_landscape"/> </RadioGroup>
初始化:
radioGroup_orientation = (RadioGroup) findViewById(R.id.rg_Orientation);
給初始化完成的RadioGroup設置監聽
radioGroup_orientation.setOnCheckedChangeListener(radioGrouplisten);
監聽的具體邏輯
//RadioGroup控件的初始化 private RadioGroup.OnCheckedChangeListener radioGrouplisten = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { int id = group.getCheckedRadioButtonId(); switch (group.getCheckedRadioButtonId()) { case R.id.rb_Landscape: orientation = Orientation.landscape; Log.i("orientation",orientation.toString()); //Toast.makeText(PrintSettingActivity.this, orientation.toString(), Toast.LENGTH_SHORT).show(); break; case R.id.rb_Portrait: orientation = Orientation.Portrait; Log.i("orientation",orientation.toString()); //Toast.makeText(PrintSettingActivity.this, orientation.toString(), Toast.LENGTH_SHORT).show(); break; default: break; } } };
RadioGroup的所有單選按鈕均不可點擊,需要遍歷RadioGroup中的每一個單選按鈕,可以根據限定條件來對按鈕進行控制
public void disableRadioGroup(RadioGroup radioGroup,String fileName){ if(recPath.endsWith("pdf")){ for (int i = 0; i < radioGroup.getChildCount(); i++) { radioGroup.getChildAt(i).setEnabled(false); } } }
三總結: