Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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();
    }
};

大佬总结

以上是大佬教程为你收集整理的Android单选按钮全部内容,希望文章能够帮你解决Android单选按钮所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。