####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);
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 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 getRes_datas() {
return res_datas;
}
public void setRes_datas(List 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 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 getOaResearchDdList() {
return oaResearchDdList;
}
public void setOaResearchDdList(List 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