[英]How to get the selected index of a RadioGroup in Android
有沒有一種簡單的方法可以在 Android 中獲取 RadioGroup 的選定索引,或者我是否必須使用 OnCheckedChangeListener 來偵聽更改並保存最后一個選定索引的內容?
例如 xml:
<RadioGroup android:id="@+id/group1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="@+id/radio1" android:text="option 1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:id="@+id/radio2" android:text="option 2" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:id="@+id/radio3" android:text="option 3" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:id="@+id/radio4" android:text="option 4" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:id="@+id/radio5" android:text="option 5" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RadioGroup>
如果用戶選擇option 3
,我想獲取索引 2。
你應該能夠做這樣的事情:
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
如果RadioGroup
包含其他視圖(如TextView
),則indexOfChild()
方法將返回錯誤的索引。
要在RadioGroup
上獲取選定的RadioButton
文本:
RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
String selectedtext = r.getText().toString();
這應該有效,
int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));
您可以引用單選組並使用getCheckedRadioButtonId ()
獲取選中的單選按鈕 ID。 看看這里
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group);
然后當您需要獲取選定的無線電選項時。
int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
if (checkedRadioButtonId == -1) {
// No item selected
}
else{
if (checkedRadioButtonId == R.id.radio_button1) {
// Do something with the button
}
}
嘗試這個
RadioGroup group= (RadioGroup) getView().findViewById(R.id.radioGroup);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
View radioButton = radioGroup.findViewById(i);
int index = radioGroup.indexOfChild(radioButton);
}
});
您可以使用 OnCheckedChangeListener 或使用 getCheckedRadioButtonId ()
您可以使用:
RadioButton rb = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
//用於獲取選中項的id
int selectedID = myRadioGroup.getCheckedRadioButtonId();
//獲取選中項的視圖
View selectedView = (View)findViewById( selectedID);
它以這種方式對我來說非常有效:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int radioButtonID = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
String selectedtext = (String) radioButton.getText();
您只需要先為 RadioButton 設置值,例如:
RadioButton radioButton = (RadioButton)findViewById(R.id.radioButton);
radioButton.setId(1); //some int value
然后每當這個空間單選按鈕被選擇時,你就可以通過你給它的 Id 來提取它的值
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
int whichIndex = radioGroup.getCheckedRadioButtonId(); //of course the radioButton
//should be inside the "radioGroup"
//in the XML
干杯!
radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);
btnDisplay=(Button)findViewById(R.id.button);
btnDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId=radioSexGroup.getCheckedRadioButtonId();
radioSexButton=(RadioButton)findViewById(selectedId);
Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show();
}
});
聚會遲到了,但這里是 @Tarek360 的 Kotlin 答案的簡化版,它適用於可能包含非 RadioButtons 的RadioGroup
:
val RadioGroup.checkedIndex: Int
get() = children
.filter { it is RadioButton }
.indexOfFirst { it.id == checkedRadioButtonId }
如果你是RadioFroup
肯定只有RadioButton
s 那么這可以是一個簡單的:
private val RadioGroup.checkedIndex =
children.indexOfFirst { it.id == checkedRadioButtonId }
那么你就沒有findViewById
的開銷。
int index_selected = radio_grp.indexOfChild(radio_grp
.findViewById(radio_grp.getCheckedRadioButtonId()));
只需使用這個:
int index = 2;
boolean option3Checked = radioGroup.getCheckedRadioButtonId() == radioGroup.getChildAt(2).getId();
這是一個Kotlin擴展,即使您的組包含 TextView 或任何非 RadioButton,也可以獲得正確的位置。
fun RadioGroup.getCheckedRadioButtonPosition(): Int {
val radioButtonId = checkedRadioButtonId
return children.filter { it is RadioButton }
.mapIndexed { index: Int, view: View ->
index to view
}.firstOrNull {
it.second.id == radioButtonId
}?.first ?: -1
}
你可以做
findViewById
來自廣播組。
這是示例:
((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);
你可以簡單地
- 從 onCreate 方法中聲明單選組和單選按鈕
private RadioGroup GrupName;
private RadioButton NameButton;
- 在 onCreate 方法中設置視圖 ID
GrupName = (RadioGroup) findViewById(R.id.radioGroupid);
- 使用選擇的單選按鈕 ID
int id = GroupName.getCheckedRadioButtonId();
- 使用 id 來匹配 buttonselected 視圖
NameButton = (RadioButton) findViewById(id);
- 最終獲取單選按鈕的值
String valueExpected = NameButton.getText().toString();
--- PS:如果你想要一個 INT 值,那么你可以投射它
int valueExpectedIn = Integer.parseInt(valueExpected);
科特林:
val selectedIndex = radioButtonGroup?.indexOfChild(
radioButtonGroup?.findViewById(
radioButtonGroup.getCheckedRadioButtonId())
)
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show();
}
});
您已經為每個單選按鈕分配了一個 id。 使用 case 塊,您可以手動檢測選定的單選按鈕,或者使用 OnCheckedChangeListener 也可以檢測到它。
xml:
<RadioGroup
android:id="@+id/rd_group_pick_reason"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/ti_titel"
android:orientation="horizontal"
android:paddingHorizontal="16dp"
>
<com.google.android.material.radiobutton.MaterialRadioButton
android:id="@+id/rd_not_deliverable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/txt_not_deliverable"
android:checked="true"
/>
<com.google.android.material.radiobutton.MaterialRadioButton
android:id="@+id/rd_after_pack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/txt_after_pack"
/>
</RadioGroup>
java:
final RadioGroup radioGroup = dialogView.findViewById(R.id.rd_group_pick_reason);
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.rd_not_deliverable:
// TODO
break;
case R.id.rd_after_pack:
// TODO
break;
}
或者
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
switch (group.getCheckedRadioButtonId()) {
case R.id.rd_not_deliverable:
System.out.println("not deliverable");
break;
case R.id.rd_after_pack:
System.out.println("after pack");
break;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.