Android 单选按钮

发布于 2024-09-11 06:25:07 字数 828 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-09-18 06:25:07

您可以使用 isChecked() 函数测试单选按钮。

例如:

if(radio1_red.isChecked())
{
       txtView.setText("Red button is checked");
}

看看这个示例

您还可以参考 android- 中给出的 页面 - 表单内容 sdk页面。

执行此操作是为了获取选定的单选按钮及其值:

 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();
    }
};

you can test the radion button with the isChecked() function.

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.

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();
    }
};
送舟行 2024-09-18 06:25:07

就像你在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文