「Android問卷調查類型頁面及邏輯實現(xiàn)」RadioButton、CheckBox澡罚、EditView伸但、單選、多選留搔、輸入更胖、

Android問卷調查類型頁面及邏輯實現(xiàn),RadioButton催式,CheckBox函喉,EditView,RadioButton+EditView單選荣月、多選、輸入梳毙、單選加輸入四種狀態(tài)四種類型

問題簡述:App做了一個類似問卷調查類型的功能哺窄,用來調研投票用的,RadioButton單選账锹,CheckBox多選萌业,EditView輸入,RadioButton+EditView單選加輸入四種類型奸柬,剛開始做遇到跟多坑生年,RecyclerView+item+LinearLayout+RadioGroup試了一下,RadioGroup有一點不可描述的問題廓奕,我就換成RecyclerView+item +RecyclerView+item+RadioButton了抱婉,主要是單選不好搞,輸入和多選還可以桌粉,還是沒搞定蒸绩,因為過于相信RecyclerView的強大,最后才試的LinearLayout動態(tài)添加布局铃肯,然而發(fā)現(xiàn)還是不行o(╥﹏╥)o患亿,第一次展示的時候沒問題,基本都是選擇完問題第二次回拉的時候單選框不在選中狀態(tài)了押逼,最后在大佬的幫助下才得以解決步藕,網上資料比較少惦界,特意做一下記錄。


單選咙冗、多選表锻、單選加輸入效果圖.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("分數(shù)區(qū)間=" + 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參數(shù)到實體類里面,這是關鍵點仪或,List實現(xiàn)也是如此确镊,再次調用取出實體類里保存的值進行判斷。

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末范删,一起剝皮案震驚了整個濱河市蕾域,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌到旦,老刑警劉巖旨巷,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異添忘,居然都是意外死亡采呐,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門搁骑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來斧吐,“玉大人,你說我怎么就攤上這事仲器∶郝剩” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵乏冀,是天一觀的道長蝶糯。 經常有香客問我,道長辆沦,這世上最難降的妖魔是什么昼捍? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮众辨,結果婚禮上端三,老公的妹妹穿的比我還像新娘。我一直安慰自己鹃彻,他們只是感情好郊闯,可當我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般团赁。 火紅的嫁衣襯著肌膚如雪育拨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天欢摄,我揣著相機與錄音熬丧,去河邊找鬼。 笑死怀挠,一個胖子當著我的面吹牛析蝴,可吹牛的內容都是我干的。 我是一名探鬼主播绿淋,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼闷畸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了吞滞?” 一聲冷哼從身側響起佑菩,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎裁赠,沒想到半個月后殿漠,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡佩捞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年绞幌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片失尖。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡啊奄,死狀恐怖,靈堂內的尸體忽然破棺而出掀潮,到底是詐尸還是另有隱情,我是刑警寧澤琼富,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布仪吧,位于F島的核電站,受9級特大地震影響鞠眉,放射性物質發(fā)生泄漏薯鼠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一械蹋、第九天 我趴在偏房一處隱蔽的房頂上張望出皇。 院中可真熱鬧,春花似錦哗戈、人聲如沸郊艘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纱注。三九已至畏浆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間狞贱,已是汗流浹背刻获。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瞎嬉,地道東北人蝎毡。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像氧枣,于是被迫代替她去往敵國和親沐兵。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內容