Android 单选按钮
我在 Android 中有一个单选按钮组,看起来像:
选择颜色:
- 红色、
- 蓝色、
- 橙色、
- 绿色
我需要获取选定的单选按钮及其值。
我在 radiogroup rg
中以这种方式有 4 个单选按钮
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
}
});
I have a radio button group in Android which looks something like:
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
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
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
isChecked()
函数测试单选按钮。例如:
看看这个示例。
您还可以参考 android- 中给出的 页面 - 表单内容 sdk页面。
执行此操作是为了获取选定的单选按钮及其值:
you can test the radion button with the
isChecked()
function.for ex:
Have a look at this Example .
You can also refer this Page - Form Stuff given in android-sdk pages.
Do this for getting selected radio button and also its value:
就像你在 android 文档中看到的那样
http://developer.android.com/reference/android/widget/ RadioGroup.OnCheckedChangeListener.html
OnCheckedChangeListener 只有方法
onCheckedChanged(RadioGroup group, int checkId) 并且不包含该方法
public void onCheckedChanged(CompoundButton arg0, boolean arg1) ->;将其删除并重试。
您可以在这里找到一个示例:
http://developer.android.com/guide/tutorials/views/ hello-formstuff.html
问候
like you can see on the android documentation
http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
the 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.
You find an example here:
http://developer.android.com/guide/tutorials/views/hello-formstuff.html
regards