【问题标题】:How to add radio buttons to radio group如何将单选按钮添加到单选组
【发布时间】:2012-05-20 21:55:52
【问题描述】:

我有一个 TableLayout,我想在每一行的第三列放置一个单选组。 我像这样构建 RadioButtons:

rg = (RadioGroup) findViewById(R.id.radioGroup1);

for (int k = 0; k < size; k++) {
    rb[k] = new RadioButton(context);
    rg.addView(rb[k]);
}

但是这会导致我的应用崩溃,有什么想法吗?

【问题讨论】:

  • 您面临的具体问题是什么?将单选组添加到表格布局或将单选按钮添加到单选组?
  • 第二个。单选按钮没问题。我不能把它们组成一个组,所以一次只能选择一个。每次我添加“rg.addView(rb[k] );"它崩溃了。
  • 请在您定义rb的位置发布。
  • rb = new RadioButton[megethos];这是 for 循环上方的某行。如果你愿意,我可以上传整个代码

标签: android radio-button


【解决方案1】:

您正在构建一个长度为megethos 的原始数组,但您的循环使用长度size。如果 megethossize 是不同的值,这可能会导致许多不同类型的错误......但是所有这些都是多余的,因为 RadioGroup 会为您保持这个数组是最新的。

我会尝试这样的:

RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton button;
for(int i = 0; i < 3; i++) {
    button = new RadioButton(this);
    button.setText("Button " + i);
    group.addView(button);
}

当你想在index 引用一个按钮时:

group.getChildAt(index);

另外,请始终发布您的 logcat 错误,它准确地告诉我们出了什么问题以及在哪里查看。否则我们只能这样猜测。

更新

错误是因为您试图将同一个按钮添加到两个不同的布局中:

tr[k].addView(rb[k]);
rg.addView(rb[k]);

一个视图只能有一个父视图。据我所知,如果没有首先进行大量定制,您无法将 RadioGroup 拆分为多个视图。然而,ListView 已经具有类似于 RadioGroup 的内置功能 setChoiceMode():

List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, list);
ListView listView = (ListView) findViewById(R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(adapter);

您可以轻松调整simple_list_item_checked 以显示 SSID 和信号强度。希望有帮助。 (如果您等待足够长的时间,imran khan 可能会通过图形更改剪切并粘贴我的答案,然后再次将其声明为他自己的。)

【讨论】:

  • 好的,我明白你要做什么了。但是这些是相同的链接,您能否将链接重新发布到您的 logcat 错误。
  • logcat 把我引向了它。我更新了我的答案。发布 logcat 的一种简单方法是突出显示错误行并按 crtl-c,然后您可以将其粘贴到原始问题并使用 crtl-k 快速格式化。
  • 这就是我一直在寻找的。我认为对于我的应用程序来说,这个列表比 Table 更好。真的,非常感谢。
【解决方案2】:

我创建了一个演示应用程序,其中我动态添加了单选按钮并处理了点击事件。

公共类 MainActivity 扩展 AppCompatActivity {

RadioGroup radioGroup2;
ArrayList<String> buttonNames;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
    buttonNames = new ArrayList<>();
    buttonNames.add("No Tip");
    buttonNames.add("10%");
    buttonNames.add("20%");
    buttonNames.add("30%");
    radioGroup2.setWeightSum(Float.parseFloat(buttonNames.size() + ""));
    radioGroup2.setBackgroundColor(Color.BLUE);


    for (int i = 0; i < buttonNames.size(); ++i) {

        RadioButton radioButton = new RadioButton(this);
        radioButton.setId(i);
        RadioGroup.LayoutParams childParam1 = new RadioGroup.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
        childParam1.setMarginEnd(2);
        radioButton.setGravity(Gravity.CENTER);
        radioButton.setLayoutParams(childParam1);
        radioButton.setBackground(null);
        radioButton.setText(buttonNames.get(i));
        radioButton.setTextColor(Color.BLUE);
        radioButton.setBackgroundColor(Color.WHITE);
        radioButton.setButtonDrawable(null);
        if (buttonNames.get(i).equals("20%")) {
            radioButton.setChecked(true);
            radioButton.setBackgroundColor(Color.BLUE);
            radioButton.setTextColor(Color.WHITE);
        }
        radioGroup2.addView(radioButton, childParam1);

    }


    radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i1) {
            RadioButton button = null;

            for (int i = 0; i < buttonNames.size(); ++i) {
                if (i1 == i) {
                    button = (RadioButton) findViewById(i1);
                    button.setChecked(true);
                    button.setBackgroundColor(Color.BLUE);
                    button.setTextColor(Color.WHITE);
                    Toast.makeText(getApplicationContext(), button.getText() + " checked", Toast.LENGTH_SHORT).show();
                } else {
                    button = (RadioButton) findViewById(i);
                    button.setChecked(false);
                    button.setBackgroundColor(Color.WHITE);
                    button.setTextColor(Color.BLUE);
                }
            }
        }
    });


}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2013-08-09
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多