微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android单选按钮

我在 Android中有一个单选按钮组,它看起来像:

选择颜色:

>红色
>蓝色
>橙色
>绿色

我需要选择单选按钮及其值.

我在radiogroup rg中以这种方式有4个radiobutton

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

    }
});

解决方法

你可以使用isChecked()函数测试radion按钮.

对于前:

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

看看这个 Example .

你也可以参考android-sdk页面中给出的这个 Page – Form Stuff .

这样做是为了获取选定的单选按钮及其值:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐