android - 动态单选按钮

标签 android

我有一个编辑文本,具体取决于编辑文本中给出的数字我必须创建一个单选按钮但我收到错误这是我的代码

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here
        setContentView(R.layout.main);
        go=(Button)findViewById(R.id.go);
        no=(EditText)findViewById(R.id.noofradio);
        err=(TextView)findViewById(R.id.err);
        go.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
               try
               {
                   int a = Integer.parseInt(no.getText().toString());
                   RadioGroup radiogroup = new RadioGroup(MainActivity.this);
                   LinearLayout.LayoutParams rg = new RadioGroup.LayoutParams(
                           RadioGroup.LayoutParams.WRAP_CONTENT,
                           RadioGroup.LayoutParams.WRAP_CONTENT);
                  for (int i = 0; i < a; i++) {
                       radiobutton = new RadioButton(MainActivity.this);
                       radiobutton.setText(i);
                       radiobutton.setId(i);
                       radiogroup.addView(radiobutton, rg);


                   }
               }
               catch(Exception ex)
               {
                   err.setText(ex.toString());
               }
            }
        });


    }

}

这是我的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >"
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="RadioButtons"
        android:id="@+id/err"/>
      <EditText
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text=""
           android:id="@+id/noofradio">
       </EditText>
       <Button
            android:id="@+id/go"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Buttons">
        </Button>
</LinearLayout>

最佳答案

android content resource$not found exception stringresource id

我认为您的 xml 中有错误(好吧,我猜是使用未定义的字符串资源)

关于android - 动态单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4790585/

相关文章:

android - Android webview 中的 insampleSize 和 compress 有什么区别?

android - 在android中阻止来电

android remoteView 布局不工作

android - 我们如何防止服务被操作系统杀死?

android - 何时调用 MediaCodec :getOutputFormat()?

android - 自动建议在 Android 中是如何实现的?

java - 如何使用按钮从 fragment 切换选项卡?

java - 使用 HashMap 将数据解析为 JSON

android - 如何使用 "Share image using"共享 Intent 在 android 中共享图像?

android - 如何制作半透明布局?