首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未呈现单选按钮圈-可能是由于上下文原因

未呈现单选按钮圈-可能是由于上下文原因
EN

Stack Overflow用户
提问于 2018-11-24 21:07:30
回答 1查看 45关注 0票数 0

我根据Firebase中的数据创建可编程的单选按钮。单选按钮的数量可以在2-4之间:

代码语言:javascript
复制
    public void addRadioButtonsWithFirebaseAnswers(List<DocumentSnapshot> answers) {
    mPollAnswerArrayList = new ArrayList<RadioButton>();
    int indexCreated = 0;
    for (DocumentSnapshot answerSnapshot : answers) {
        Answer answer = answerSnapshot.toObject(Answer.class);
        mPollAnswerArrayList.add((indexCreated), new RadioButton((getContext())));
        RadioButton radioButton = mPollAnswerArrayList.get(indexCreated);
        radioButton.setTag(indexCreated);
        radioButton.setText(answer.getAnswer().toString());
        radioButton.setTextColor(getResources().getColor(R.color.black));
        //TODO: Investigate if this line is necessary
        radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));

        //TODO: Determne which type of RadioButton to use for consistency
        if (Build.VERSION.SDK_INT >= 21) {
        //                radioButton.setButtonTintMode(PorterDuff.Mode.DARKEN);
        } else {
            //TODO: Is this necessary? What happens?
        //                radioButton.setButtonDrawable(R.drawable.black_ring);
        }

        mPollQuestionRadioGroup.addView(radioButton, mParams);
        indexCreated++;
    }
}

我注意到在API 23上,由于某些原因,这些按钮没有出现。我仍然可以点击并获得想要的结果,但是按钮实际上并不是由UI呈现的:

编辑:这可能与我的上下文参数有关,不确定它是否与API相关,但我没有正确传递上下文。请注意,代码在片段中。当我从.getContext()改为.getApplicationContext()时,我注意到了不同之处。还有可能我没有使用支持库单选按钮?

代码语言:javascript
复制
mPollAnswerArrayList.add((indexCreated), new RadioButton((getContext())));
EN

回答 1

Stack Overflow用户

发布于 2018-12-01 16:19:51

这是最简单的方法。尝尝这个。

代码语言:javascript
复制
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 10, 10, 10); // leftMargin, topMargin, rightMargin, buttomMargin
     RadioGroup radioGroup = new RadioGroup(getContext());
     RadioButton radioButton = new RadioButton(getContext());
     radioButton.setLayoutParams(params1);
     radioButton.setId(1);
     radioButton.setText("text");
     radioButton.setPadding(0, 5, 0, 5); // leftMargin, topMargin, rightMargin, buttomMargin
     radioGroup.addView(radioButton);

使用此选项选中/选择单选按钮,

代码语言:javascript
复制
radioGroup.check(3);  

使用此选项取消选中单选按钮,

代码语言:javascript
复制
radioGroup.clearCheck();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53458459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档