Android 安卓单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3307039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Radio Button
提问by Sanjeev
I have a radio button group in Android which looks something like:
我在 Android 中有一个单选按钮组,它看起来像:
Choose Color:
选择颜色:
- Red
- Blue
- Orange
- Green
- 红色的
- 蓝色
- 橘子
- 绿
I need to get selected radio button and also its value.
我需要获得选定的单选按钮及其值。
I have 4 radiobuttons in this manner within radiogroup rg
我在radiogroup中以这种方式有4个单选按钮 rg
rb1a=(RadioButton)findViewById(R.id.rb1a);
rb1b=(RadioButton)findViewById(R.id.rb1b);
rb1c=(RadioButton)findViewById(R.id.rb1c);
rb1d=(RadioButton)findViewById(R.id.rb1d);
tv1=(TextView)findViewById(R.id.tv1);
next1=(Button)findViewById(R.id.next1);
rg=(RadioGroup)findViewById(R.id.rg);
// I have error after this line.please help
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
}
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
}
});
回答by Paresh Mayani
you can test the radion button with the isChecked()
function.
您可以使用该isChecked()
功能测试单选按钮。
for ex:
例如:
if(radio1_red.isChecked())
{
txtView.setText("Red button is checked");
}
Have a look at this Example .
看看这个例子。
You can also refer this Page - Form Stuff given in android-sdk pages.
您还可以参考此页面 -android-sdk 页面中给出的表单内容。
Do this for getting selected radio button and also its value:
执行此操作以获取选定的单选按钮及其值:
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
回答by fmo
like you can see on the android documentation http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.htmlthe OnCheckedChangeListener has only the method onCheckedChanged(RadioGroup group, int checkedId) and doesn't contain the method public void onCheckedChanged(CompoundButton arg0, boolean arg1) -> remove it and try again.
就像您在 android 文档http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html上看到的一样 ,OnCheckedChangeListener 只有方法 onCheckedChanged(RadioGroup group, int checksId) 并且不包含该方法public void onCheckedChanged(CompoundButton arg0, boolean arg1) -> 删除它并重试。
You find an example here: http://developer.android.com/guide/tutorials/views/hello-formstuff.html
你在这里找到一个例子:http: //developer.android.com/guide/tutorials/views/hello-formstuff.html
regards
问候