「Android问卷调查类型页面及逻辑实现」RadioButton、CheckBox、EditView、单选、多选、输入、

####Android问卷调查类型页面及逻辑实现,RadioButton,CheckBox,EditView,RadioButton+EditView单选、多选、输入、单选加输入四种状态四种类型

问题简述:App做了一个类似问卷调查类型的功能,用来调研投票用的,RadioButton单选,CheckBox多选,EditView输入,RadioButton+EditView单选加输入四种类型,刚开始做遇到跟多坑,RecyclerView+item+LinearLayout+RadioGroup试了一下,RadioGroup有一点不可描述的问题,我就换成RecyclerView+item +RecyclerView+item+RadioButton了,主要是单选不好搞,输入和多选还可以,还是没搞定,因为过于相信RecyclerView的强大,最后才试的LinearLayout动态添加布局,然而发现还是不行o(╥﹏╥)o,第一次展示的时候没问题,基本都是选择完问题第二次回拉的时候单选框不在选中状态了,最后在大佬的帮助下才得以解决,网上资料比较少,特意做一下记录。

单选、多选、单选加输入效果图

单选、多选、输入效果图

public class QusServAct extends AppCompatActivity {

    QusServBean bean;
    LinearLayout ll;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_qus_serv);
        //todo从网络上面获取json初始化bean
        bean = new Gson().fromJson(json, QusServBean.class);
        ll = findViewById(R.id.ll);
        for (QusServBean.ResDatasBean data : bean.getRes_datas()) {
            initTitle(data);
            switch (data.getOrd_type()) {
                case "1":
                    addEdtView(data);
                    //输入
                    break;
                case "2":
                    addSinView(data);
                    //单选
                    break;
                case "3":
                    //多选
                    addMulView(data);
                    break;
            }
        }
        Button button = new Button(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                push();
            }
        });
        button.setText("提交");
        ll.addView(button);
    }

    private void addSinView(final QusServBean.ResDatasBean data) {

        for (final QusServBean.ResDatasBean.OaResearchDdListBean subData : data.getOaResearchDdList()) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setPadding(5, 10, 10, 10);
            radioButton.setText(subData.getShow_index() + "." + subData.getOrdd_name());
            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        for (QusServBean.ResDatasBean.OaResearchDdListBean oaResearchDdListBean : data.getOaResearchDdList()) {
                            oaResearchDdListBean.getCheckBox().setChecked(false);
                        }
                        buttonView.setChecked(true);
                    }
                    for (QusServBean.ResDatasBean.OaResearchDdListBean oaResearchDdListBean : data.getOaResearchDdList()) {
                        if (oaResearchDdListBean.getCheckBox().isChecked()) {
                            if (data.getEditText() != null) {
                                data.editText.setVisibility(View.VISIBLE);
                                data.editText.setHint("分数区间=" + oaResearchDdListBean.getBegin_score() + "---" + oaResearchDdListBean.getEnd_score());
                            }
                            break;
                        }
                        if (data.getEditText() != null) {
                            data.editText.setVisibility(View.GONE);
                        }
                    }
                }
            });
            subData.setCheckBox(radioButton);
            ll.addView(radioButton);
            ((LinearLayout.LayoutParams) radioButton.getLayoutParams()).setMargins(20, 10, 10, 10);
        }
        if ("1".equals(data.getIs_score())) {
            data.setEditText(new EditText(this));
            data.getEditText().setVisibility(View.GONE);
            data.getEditText().setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

            data.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            data.getEditText().setBackgroundResource(R.drawable.edt_bg);
            ll.addView(data.getEditText());
            ((LinearLayout.LayoutParams) data.getEditText().getLayoutParams()).setMargins(20, 10, 20, 10);
        
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值