「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,第一次展示的时候没问题,基本都是选择完问题第二次回拉的时候单选框不在选中状态了,最后在大佬的帮助下才得以解决,网上资料比较少,特意做一下记录。


单选、多选、单选加输入效果图.png
单选、多选、输入效果图..png
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);
            data.getEditText().getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
        }
    }

    private void addMulView(final QusServBean.ResDatasBean data) {
        for (final QusServBean.ResDatasBean.OaResearchDdListBean subData : data.getOaResearchDdList()) {
            CheckBox checkBox = new CheckBox(this);
            checkBox.setPadding(5, 10, 10, 10);
            checkBox.setText(subData.getShow_index() + "." + subData.getOrdd_name());
            subData.setCheckBox(checkBox);
            ll.addView(checkBox);
            ((LinearLayout.LayoutParams) checkBox.getLayoutParams()).setMargins(20, 10, 10, 10);

        }
    }

    private void addEdtView(final QusServBean.ResDatasBean data) {
        data.setEditText(new EditText(this));
        ll.addView(data.getEditText());
        data.getEditText().setBackgroundResource(R.drawable.edt_bg);
        data.getEditText().setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        ((LinearLayout.LayoutParams) data.getEditText().getLayoutParams()).setMargins(20, 10, 20, 10);
        data.getEditText().getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
    }

    private void initTitle(QusServBean.ResDatasBean data) {
        TextView titleTv = new TextView(this);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        titleTv.setTextColor(0xff333333);
        titleTv.setPadding(10, 10, 10, 10);
        titleTv.setText(data.getShow_index() + ": " + data.getOrd_name());
        ll.addView(titleTv);
    }
}

实体类 Bean

(关键)transient 保存实例需要使用,也可直接set参数到实体类里面,这是关键点,List实现也是如此,再次调用取出实体类里保存的值进行判断。

class QusServBean {

    private int result;
    private String message;
    private String cause;
    private ResDataBean res_data;
    private List<ResDatasBean> res_datas;

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCause() {
        return cause;
    }

    public void setCause(String cause) {
        this.cause = cause;
    }

    public ResDataBean getRes_data() {
        return res_data;
    }

    public void setRes_data(ResDataBean res_data) {
        this.res_data = res_data;
    }

    public List<ResDatasBean> getRes_datas() {
        return res_datas;
    }

    public void setRes_datas(List<ResDatasBean> res_datas) {
        this.res_datas = res_datas;
    }

    public static class ResDataBean {

        private String app_date;
        private int avg_score;
        private int createBy;
        private String create_date;
        private long create_user;
        private String createtime;
        private String is_jg;
        private String is_sx;
        private int jg_medal_count;
        private int jg_medal_id;
        private String jg_medal_name;
        private int medal_count;
        private int medal_id;
        private String medal_name;
        private String orm_app_user_name;
        private String orm_create_user_name;
        private String orm_end_date;
        private long orm_id;
        private String orm_title;
        private long tp_m_dept_id;
        private String tp_m_dept_name;
        private long tp_m_id;
        private int tp_m_score_count;
        private String tp_m_state;
        private long tp_m_user_id;
        private String tp_m_user_name;
        private String tp_percent;
        private String tp_percent_name;
        private int updateBy;
        private String update_date;
        private long update_user;
        private String updatetime;
        private List<?> oaResearchTpDList;
        private List<?> fileList;

        public String getApp_date() {
            return app_date;
        }

        public void setApp_date(String app_date) {
            this.app_date = app_date;
        }

        public int getAvg_score() {
            return avg_score;
        }

        public void setAvg_score(int avg_score) {
            this.avg_score = avg_score;
        }

        public int getCreateBy() {
            return createBy;
        }

        public void setCreateBy(int createBy) {
            this.createBy = createBy;
        }

        public String getCreate_date() {
            return create_date;
        }

        public void setCreate_date(String create_date) {
            this.create_date = create_date;
        }

        public long getCreate_user() {
            return create_user;
        }

        public void setCreate_user(long create_user) {
            this.create_user = create_user;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getIs_jg() {
            return is_jg;
        }

        public void setIs_jg(String is_jg) {
            this.is_jg = is_jg;
        }

        public String getIs_sx() {
            return is_sx;
        }

        public void setIs_sx(String is_sx) {
            this.is_sx = is_sx;
        }

        public int getJg_medal_count() {
            return jg_medal_count;
        }

        public void setJg_medal_count(int jg_medal_count) {
            this.jg_medal_count = jg_medal_count;
        }

        public int getJg_medal_id() {
            return jg_medal_id;
        }

        public void setJg_medal_id(int jg_medal_id) {
            this.jg_medal_id = jg_medal_id;
        }

        public String getJg_medal_name() {
            return jg_medal_name;
        }

        public void setJg_medal_name(String jg_medal_name) {
            this.jg_medal_name = jg_medal_name;
        }

        public int getMedal_count() {
            return medal_count;
        }

        public void setMedal_count(int medal_count) {
            this.medal_count = medal_count;
        }

        public int getMedal_id() {
            return medal_id;
        }

        public void setMedal_id(int medal_id) {
            this.medal_id = medal_id;
        }

        public String getMedal_name() {
            return medal_name;
        }

        public void setMedal_name(String medal_name) {
            this.medal_name = medal_name;
        }

        public String getOrm_app_user_name() {
            return orm_app_user_name;
        }

        public void setOrm_app_user_name(String orm_app_user_name) {
            this.orm_app_user_name = orm_app_user_name;
        }

        public String getOrm_create_user_name() {
            return orm_create_user_name;
        }

        public void setOrm_create_user_name(String orm_create_user_name) {
            this.orm_create_user_name = orm_create_user_name;
        }

        public String getOrm_end_date() {
            return orm_end_date;
        }

        public void setOrm_end_date(String orm_end_date) {
            this.orm_end_date = orm_end_date;
        }

        public long getOrm_id() {
            return orm_id;
        }

        public void setOrm_id(long orm_id) {
            this.orm_id = orm_id;
        }

        public String getOrm_title() {
            return orm_title;
        }

        public void setOrm_title(String orm_title) {
            this.orm_title = orm_title;
        }

        public long getTp_m_dept_id() {
            return tp_m_dept_id;
        }

        public void setTp_m_dept_id(long tp_m_dept_id) {
            this.tp_m_dept_id = tp_m_dept_id;
        }

        public String getTp_m_dept_name() {
            return tp_m_dept_name;
        }

        public void setTp_m_dept_name(String tp_m_dept_name) {
            this.tp_m_dept_name = tp_m_dept_name;
        }

        public long getTp_m_id() {
            return tp_m_id;
        }

        public void setTp_m_id(long tp_m_id) {
            this.tp_m_id = tp_m_id;
        }

        public int getTp_m_score_count() {
            return tp_m_score_count;
        }

        public void setTp_m_score_count(int tp_m_score_count) {
            this.tp_m_score_count = tp_m_score_count;
        }

        public String getTp_m_state() {
            return tp_m_state;
        }

        public void setTp_m_state(String tp_m_state) {
            this.tp_m_state = tp_m_state;
        }

        public long getTp_m_user_id() {
            return tp_m_user_id;
        }

        public void setTp_m_user_id(long tp_m_user_id) {
            this.tp_m_user_id = tp_m_user_id;
        }

        public String getTp_m_user_name() {
            return tp_m_user_name;
        }

        public void setTp_m_user_name(String tp_m_user_name) {
            this.tp_m_user_name = tp_m_user_name;
        }

        public String getTp_percent() {
            return tp_percent;
        }

        public void setTp_percent(String tp_percent) {
            this.tp_percent = tp_percent;
        }

        public String getTp_percent_name() {
            return tp_percent_name;
        }

        public void setTp_percent_name(String tp_percent_name) {
            this.tp_percent_name = tp_percent_name;
        }

        public int getUpdateBy() {
            return updateBy;
        }

        public void setUpdateBy(int updateBy) {
            this.updateBy = updateBy;
        }

        public String getUpdate_date() {
            return update_date;
        }

        public void setUpdate_date(String update_date) {
            this.update_date = update_date;
        }

        public long getUpdate_user() {
            return update_user;
        }

        public void setUpdate_user(long update_user) {
            this.update_user = update_user;
        }

        public String getUpdatetime() {
            return updatetime;
        }

        public void setUpdatetime(String updatetime) {
            this.updatetime = updatetime;
        }

        public List<?> getOaResearchTpDList() {
            return oaResearchTpDList;
        }

        public void setOaResearchTpDList(List<?> oaResearchTpDList) {
            this.oaResearchTpDList = oaResearchTpDList;
        }

        public List<?> getFileList() {
            return fileList;
        }

        public void setFileList(List<?> fileList) {
            this.fileList = fileList;
        }
    }

    public static class ResDatasBean {
          
        private int createBy;
        private String createtime;
        private String is_score;
        private long ord_id;
        private String ord_name;
        private String ord_type;
        private String ordd_id;
        private String ordd_name;
        private int show_index;
        private long tp_d_id;
        private String tp_d_info;
        private int tp_d_score;
        private String tp_dept_name;
        private long tp_m_id;
        private int tp_percent;
        private String tp_user_name;
        private int updateBy;
        private String updatetime;
        private List<OaResearchDdListBean> oaResearchDdList;
        private List<?> oaResearchTpDdList;
        transient  EditText editText;
        boolean haveChooseOne = false;

        public boolean isHaveChooseOne() {
            return haveChooseOne;
        }

        public void setHaveChooseOne(boolean haveChooseOne) {
            this.haveChooseOne = haveChooseOne;
        }

        public EditText getEditText() {
            return editText;
        }

        public void setEditText(EditText editText) {
            this.editText = editText;
        }

        public int getCreateBy() {
            return createBy;
        }

        public void setCreateBy(int createBy) {
            this.createBy = createBy;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getIs_score() {
            return is_score;
        }

        public void setIs_score(String is_score) {
            this.is_score = is_score;
        }

        public long getOrd_id() {
            return ord_id;
        }

        public void setOrd_id(long ord_id) {
            this.ord_id = ord_id;
        }

        public String getOrd_name() {
            return ord_name;
        }

        public void setOrd_name(String ord_name) {
            this.ord_name = ord_name;
        }

        public String getOrd_type() {
            return ord_type;
        }

        public void setOrd_type(String ord_type) {
            this.ord_type = ord_type;
        }

        public String getOrdd_id() {
            return ordd_id;
        }

        public void setOrdd_id(String ordd_id) {
            this.ordd_id = ordd_id;
        }

        public String getOrdd_name() {
            return ordd_name;
        }

        public void setOrdd_name(String ordd_name) {
            this.ordd_name = ordd_name;
        }

        public int getShow_index() {
            return show_index;
        }

        public void setShow_index(int show_index) {
            this.show_index = show_index;
        }

        public long getTp_d_id() {
            return tp_d_id;
        }

        public void setTp_d_id(long tp_d_id) {
            this.tp_d_id = tp_d_id;
        }

        public String getTp_d_info() {
            return tp_d_info;
        }

        public void setTp_d_info(String tp_d_info) {
            this.tp_d_info = tp_d_info;
        }

        public int getTp_d_score() {
            return tp_d_score;
        }

        public void setTp_d_score(int tp_d_score) {
            this.tp_d_score = tp_d_score;
        }

        public String getTp_dept_name() {
            return tp_dept_name;
        }

        public void setTp_dept_name(String tp_dept_name) {
            this.tp_dept_name = tp_dept_name;
        }

        public long getTp_m_id() {
            return tp_m_id;
        }

        public void setTp_m_id(long tp_m_id) {
            this.tp_m_id = tp_m_id;
        }

        public int getTp_percent() {
            return tp_percent;
        }

        public void setTp_percent(int tp_percent) {
            this.tp_percent = tp_percent;
        }

        public String getTp_user_name() {
            return tp_user_name;
        }

        public void setTp_user_name(String tp_user_name) {
            this.tp_user_name = tp_user_name;
        }

        public int getUpdateBy() {
            return updateBy;
        }

        public void setUpdateBy(int updateBy) {
            this.updateBy = updateBy;
        }

        public String getUpdatetime() {
            return updatetime;
        }

        public void setUpdatetime(String updatetime) {
            this.updatetime = updatetime;
        }

        public List<OaResearchDdListBean> getOaResearchDdList() {
            return oaResearchDdList;
        }

        public void setOaResearchDdList(List<OaResearchDdListBean> oaResearchDdList) {
            this.oaResearchDdList = oaResearchDdList;
        }

        public List<?> getOaResearchTpDdList() {
            return oaResearchTpDdList;
        }

        public void setOaResearchTpDdList(List<?> oaResearchTpDdList) {
            this.oaResearchTpDdList = oaResearchTpDdList;
        }

        public static class OaResearchDdListBean {
         
            private int begin_score;
            private int createBy;
            private String createtime;
            private int end_score;
            private long ord_id;
            private long ordd_id;
            private String ordd_name;
            private long orm_id;
            private int show_index;
            private int updateBy;
            private String updatetime;
            private transient CompoundButton checkBox;

            public CompoundButton getCheckBox() {
                return checkBox;
            }

            public void setCheckBox(CompoundButton checkBox) {
                this.checkBox = checkBox;
            }

            public int getBegin_score() {
                return begin_score;
            }

            public void setBegin_score(int begin_score) {
                this.begin_score = begin_score;
            }

            public int getCreateBy() {
                return createBy;
            }

            public void setCreateBy(int createBy) {
                this.createBy = createBy;
            }

            public String getCreatetime() {
                return createtime;
            }

            public void setCreatetime(String createtime) {
                this.createtime = createtime;
            }

            public int getEnd_score() {
                return end_score;
            }

            public void setEnd_score(int end_score) {
                this.end_score = end_score;
            }

            public long getOrd_id() {
                return ord_id;
            }

            public void setOrd_id(long ord_id) {
                this.ord_id = ord_id;
            }

            public long getOrdd_id() {
                return ordd_id;
            }

            public void setOrdd_id(long ordd_id) {
                this.ordd_id = ordd_id;
            }

            public String getOrdd_name() {
                return ordd_name;
            }

            public void setOrdd_name(String ordd_name) {
                this.ordd_name = ordd_name;
            }

            public long getOrm_id() {
                return orm_id;
            }

            public void setOrm_id(long orm_id) {
                this.orm_id = orm_id;
            }

            public int getShow_index() {
                return show_index;
            }

            public void setShow_index(int show_index) {
                this.show_index = show_index;
            }

            public int getUpdateBy() {
                return updateBy;
            }

            public void setUpdateBy(int updateBy) {
                this.updateBy = updateBy;
            }

            public String getUpdatetime() {
                return updatetime;
            }

            public void setUpdatetime(String updatetime) {
                this.updatetime = updatetime;
            }
        }
    }
}

Demo地址仅供参考
https://github.com/LaoXiZi/AddViewDemo.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容