Joh*_*ker 201 java xml android radio-group
有没有一种简单的方法可以在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>
Run Code Online (Sandbox Code Playgroud)
如果用户选择option 3我想获得索引,2.
BP.*_*BP. 467
你应该可以做这样的事情:
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
Run Code Online (Sandbox Code Playgroud)
如果RadioGroup包含其他视图(如a TextView)那么该indexOfChild()方法将返回错误的索引.
要获取所选RadioButton文本RadioGroup:
RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
String selectedtext = r.getText().toString();
Run Code Online (Sandbox Code Playgroud)
sou*_*rar 107
这应该工作,
int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));
Run Code Online (Sandbox Code Playgroud)
Ste*_*aly 56
您可以引用无线电组并使用getCheckedRadioButtonId ()获取已检查的单选按钮ID.看看这里
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group);
Run Code Online (Sandbox Code Playgroud)
然后,当您需要获取所选的无线电选项.
int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
if (checkedRadioButtonId == -1) {
// No item selected
}
else{
if (checkedRadioButtonId == R.id.radio_button1) {
// Do something with the button
}
}
Run Code Online (Sandbox Code Playgroud)
Noo*_*ooh 24
试试这个
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);
}
});
Run Code Online (Sandbox Code Playgroud)
您可以使用:
RadioButton rb = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
Run Code Online (Sandbox Code Playgroud)
小智 6
//用于获取所选项目的ID
int selectedID = myRadioGroup.getCheckedRadioButtonId();
Run Code Online (Sandbox Code Playgroud)
//获取所选项目的视图
View selectedView = (View)findViewById( selectedID);
Run Code Online (Sandbox Code Playgroud)
它以这种方式对我很有效:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int radioButtonID = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
String selectedtext = (String) radioButton.getText();
Run Code Online (Sandbox Code Playgroud)
您所需要的只是首先将值设置为您的RadioButton,例如:
RadioButton radioButton = (RadioButton)findViewById(R.id.radioButton);
radioButton.setId(1); //some int value
Run Code Online (Sandbox Code Playgroud)
然后每当选择这个空间radioButton时,你可以通过你给它的Id来拉取它的值
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
int whichIndex = radioGroup.getCheckedRadioButtonId(); //of course the radioButton
//should be inside the "radioGroup"
//in the XML
Run Code Online (Sandbox Code Playgroud)
干杯!
小智 5
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();
}
});
Run Code Online (Sandbox Code Playgroud)
科特林:
val selectedIndex = radioButtonGroup?.indexOfChild(
radioButtonGroup?.findViewById(
radioButtonGroup.getCheckedRadioButtonId())
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
203971 次 |
| 最近记录: |