Android的单选按钮行为不端的模拟器上只 [英] Android RadioButtons misbehaving on Emulator only

查看:165
本文介绍了Android的单选按钮行为不端的模拟器上只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我RadioGroup中的XML。但我是从单选按钮code填充它。
问题是,在仿真器单选按钮不规矩w.r.t RadioGroup中。我可以一次选择所有单选按钮。但是,一切都似乎在真实的设备做工精细。

下面是截图和code所有文件:

截图从模拟器

截图从设备三星Galaxy

来源为MainActivity.java

 公共类MainActivity延伸活动{
/ **当第一次创建活动调用。 * /
私人RadioGroup中RG;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    LayoutInflater充气= LayoutInflater.from(getBaseContext());
    RG =(RadioGroup中)findViewById(R.id.rg);    的for(int i = 0;我小于5;我++){
        inflater.inflate(R.layout.myradio,RG,真正的);
    }}

}

的main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:ID =@ + ID / llContainer
    工具:上下文=MainActivity。>
< RadioGroup中
    机器人:ID =@ + ID / RG
    机器人:背景=#0000FF
    机器人:方向=垂直
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =FILL_PARENT>< / RadioGroup中>

myradio.xml

 <?XML版本=1.0编码=UTF-8&GT?;<单选按钮的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:文字=单选/>


解决方案

这会是这样

 公共类示例扩展活动{
    / **当第一次创建活动调用。 * /
    私人RadioGroup中RGROUP;
     单选RB;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.example);
        RGROUP =(RadioGroup中)findViewById(R.id.rg);
        最终单选[]的rb =新单选[5];
        的for(int i = 0;我小于5;我++){
            RB [I] =新的单选按钮(本);
            RB [I] .setText(RDO+ I);
            RB [I] .setId(ⅰ);
            rgroup.addView(RB [I]);
        }    }    }

I have RadioGroup in xml. And I am filling it with RadioButton from code. Problem is, in Emulators RadioButtons do not behave w.r.t RadioGroup. I can select all radiobuttons at once. But everything seems to work fine on real device.

Below are screenshots and code for all files:

Screenshot from emulator

Screenshot from device Samsung Galaxy

Source for MainActivity.java

public class MainActivity extends Activity {
/** Called when the activity is first created. */
private RadioGroup rg;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LayoutInflater inflater = LayoutInflater.from(getBaseContext());
    rg = (RadioGroup)findViewById(R.id.rg);

    for (int i = 0; i < 5; i++) {
        inflater.inflate(R.layout.myradio, rg,true);
    }

}

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/llContainer"
    tools:context=".MainActivity" >


<RadioGroup 
    android:id="@+id/rg"
    android:background="#0000ff"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

</RadioGroup>

myradio.xml

<?xml version="1.0" encoding="utf-8"?>

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="RadioButton" />

解决方案

It will be like this

public class example extends Activity {
    /** Called when the activity is first created. */
    private RadioGroup rgroup;
     RadioButton rb;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.example);


        rgroup = (RadioGroup)findViewById(R.id.rg);
        final RadioButton[] rb = new RadioButton[5];
        for (int i = 0; i < 5; i++) {
            rb[i]=new RadioButton(this);
            rb[i].setText("rdo"+i);
            rb[i].setId(i);
            rgroup.addView(rb[i]);


        }

    }

    }

这篇关于Android的单选按钮行为不端的模拟器上只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆