【问题标题】:Android RadioButtons misbehaving on Emulator onlyAndroid RadioButtons 仅在模拟器上行为不端
【发布时间】:2013-01-04 09:10:24
【问题描述】:

我在 xml 中有 RadioGroup。我正在用代码中的 RadioButton 填充它。 问题是,在模拟器中,RadioButtons 的行为与 RadioGroup 不同。我可以一次选择所有单选按钮。但在真实设备上似乎一切正常。

以下是所有文件的截图和代码:

模拟器截图

Samsung Galaxy 设备的屏幕截图

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

}

}

ma​​in.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" />

【问题讨论】:

  • @Avadhani 顺便说一句,布局不是我关心的问题,而是我的行为。

标签: android android-layout android-emulator


【解决方案1】:

会是这样的

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]);


        }

    }

    }

【讨论】:

  • 我不想改变创建布局的方法。因为我的需求是动态的。另外,您可能已经注意到此代码在设备上运行良好。
  • @Avinazz .k...我会尝试不同的
  • 我对使用任何方法生成此布局都没有问题,但它必须是动态的。我可以用几种不同的方式做同样的事情,但模拟器仍然抛弃我。顺便说一句,我在 Android 上工作 2 年了,但我仍然犯愚蠢的错误,我认为这里不是这种情况。
  • 当然,如果您最终出现同样的行为,请告诉我。
  • 哇,工作。公认。我所要做的就是为它分配一些 id。但我仍然想知道为什么它在模拟器上的行为与真实设备不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-30
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
  • 2020-06-15
相关资源
最近更新 更多