android 实现调查问卷-单选-多选
非常久没写东西了。今天来总结下有关android调查问卷的需求实现。
转载请加地址:http://blog.csdn.net/jing110fei/article/details/46618229
先上效果图
个人分析,最好是用动态布局载入来实现,好了。说思路。将这总体分为3块
最外面这个布局里面。依据第二层问题的数量来动态生成布局,增加在第一层布局里面,
然后再依据问题下答案的数量来动态生成布局。增加第二层布局里面,思路这么透彻,想想还有些小激动呢。
先建造三个实体类
public class Page {
//问卷id
private String pageId;
//问卷状态
private String status;
//问卷主题
private String title;
//题目
private ArrayList<Quesition> quesitions; public ArrayList<Quesition> getQuesitions() {
return quesitions;
}
public void setQuesitions(ArrayList<Quesition> quesitions) {
this.quesitions = quesitions;
} public String getPageId() {
return pageId;
}
public void setPageId(String pageId) {
this.pageId = pageId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} }
public class Quesition {
//题目id
private String quesitionId;
//单选多选标识
private String type;
//题目
private String content;
//选项
private ArrayList<Answer> answers;
//是否解答
private int que_state; public int getQue_state() {
return que_state;
}
public void setQue_state(int que_state) {
this.que_state = que_state;
} public String getQuesitionId() {
return quesitionId;
}
public void setQuesitionId(String quesitionId) {
this.quesitionId = quesitionId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public ArrayList<Answer> getAnswers() {
return answers;
}
public void setAnswers(ArrayList<Answer> answers) {
this.answers = answers;
} }
public class Answer {
//答案id
private String answerId;
//答案主体
private String answer_content;
//答案是否被解答
private int ans_state; public int getAns_state() {
return ans_state;
}
public void setAns_state(int ans_state) {
this.ans_state = ans_state;
}
public String getAnswerId() {
return answerId;
}
public void setAnswerId(String answerId) {
this.answerId = answerId;
}
public String getAnswer_content() {
return answer_content;
}
public void setAnswer_content(String answer_content) {
this.answer_content = answer_content;
} }
建造这三个实体类的目的是为了在做demo的时候直接通过假数据来尽可能的贴近项目。使demo完毕后能尽快的移植进项目。
以下来看看布局,总工用到了3个布局。
首先是activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e6e4e3" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EA5514"
android:orientation="horizontal" >
<ImageView
android:id="@+id/test_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:background="@drawable/ic_back_white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="调查问卷"
android:textSize="18sp"
android:textColor="@android:color/white"
android:layout_gravity="center"
android:gravity="center"/>
</LinearLayout>
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:textColor="#898989"
/>
<LinearLayout
android:id="@+id/lly_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginBottom="30dp"
android:text="提交"
android:textSize="20sp"
android:textColor="@android:color/white"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/button_submit"/>
</LinearLayout>
</LinearLayout> </ScrollView>
id为lly_test的布局就是终于要增加的目的布局
然后是quesition_layout.xml
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="35dp" >
<TextView
android:id="@+id/txt_question_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#3e3a39"
android:layout_marginLeft="45dp"
/>
<LinearLayout
android:id="@+id/lly_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp" android:background="@drawable/shape_dialog_radius_all"
> </LinearLayout> </LinearLayout>
//然后是answer_layout.xml
<? xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/lly_answer_size"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/txt_answer_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#595757"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<View
android:id="@+id/vw_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#9EA0A0"
> </View>
</LinearLayout>
然后是主要代码。长久不写博客,有点生疏了,大家顺着思路来看,凝视也差点儿相同详尽,假设有不明确的再讨论
public class MainActivity extends Activity {
private LinearLayout test_layout;
private Page the_page;
//答案列表
private ArrayList<Answer> the_answer_list;
//问题列表
private ArrayList<Quesition> the_quesition_list;
//问题所在的View
private View que_view;
//答案所在的View
private View ans_view;
private LayoutInflater xInflater;
private Page page;
//以下这两个list是为了实现点击的时候改变图片。由于单选多选时情况不一样。为了方便控制
//存每一个问题下的imageview
private ArrayList<ArrayList<ImageView>> imglist=new ArrayList<ArrayList<ImageView>>();
//存每一个答案的imageview
private ArrayList<ImageView> imglist2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//假数据
initDate();
//提交按钮
Button button=(Button)findViewById(R.id.submit);
button.setOnClickListener(new submitOnClickListener(page));
}
private void initDate() {
//假数据
// TODO Auto-generated method stub
Answer a_one=new Answer();
a_one.setAnswerId("0");
a_one.setAnswer_content("男");
a_one.setAns_state(0);
Answer a_two=new Answer();
a_two.setAnswerId("1");
a_two.setAnswer_content("女");
a_two.setAns_state(0); Answer a_three=new Answer();
a_three.setAnswerId("3");
a_three.setAnswer_content("是");
a_three.setAns_state(0);
Answer a_four=new Answer();
a_four.setAnswerId("4");
a_four.setAnswer_content("不是");
a_four.setAns_state(0); Answer a_three1=new Answer();
a_three1.setAnswerId("3");
a_three1.setAnswer_content("是");
a_three1.setAns_state(0);
Answer a_four1=new Answer();
a_four1.setAnswerId("4");
a_four1.setAnswer_content("不是");
a_four1.setAns_state(0); ArrayList<Answer> answers_one=new ArrayList<Answer>();
answers_one.add(a_one);
answers_one.add(a_two); ArrayList<Answer> answers_two=new ArrayList<Answer>();
answers_two.add(a_one);
answers_two.add(a_two);
answers_two.add(a_three);
answers_two.add(a_four); ArrayList<Answer> answers_three=new ArrayList<Answer>();
answers_three.add(a_one);
answers_three.add(a_two);
answers_three.add(a_three);
answers_three.add(a_four);
answers_three.add(a_three1);
answers_three.add(a_four1); Quesition q_one=new Quesition();
q_one.setQuesitionId("00");
q_one.setType("0");
q_one.setContent("1、您的性别:");
q_one.setAnswers(answers_one);
q_one.setQue_state(0); Quesition q_two=new Quesition();
q_two.setQuesitionId("01");
q_two.setType("1");
q_two.setContent("2、您是党员吗?");
q_two.setAnswers(answers_two);
q_two.setQue_state(0); Quesition q_three=new Quesition();
q_three.setQuesitionId("03");
q_three.setType("1");
q_three.setContent("3、您是dsfsdfsd吗?");
q_three.setAnswers(answers_three);
q_three.setQue_state(0); ArrayList<Quesition> quesitions=new ArrayList<Quesition>();
quesitions.add(q_one);
quesitions.add(q_two);
quesitions.add(q_three); page=new Page();
page.setPageId("000");
page.setStatus("0");
page.setTitle("第一次调查问卷");
page.setQuesitions(quesitions);
//载入布局
initView(page);
}
private void initView(Page page) {
// TODO Auto-generated method stub
//这是要把问题的动态布局增加的布局
test_layout=(LinearLayout)findViewById(R.id.lly_test);
TextView page_txt=(TextView)findViewById(R.id.txt_title);
page_txt.setText(page.getTitle());
//获得问题即第二层的数据
the_quesition_list=page.getQuesitions();
//依据第二层问题的多少,来动态载入布局
for(int i=0;i<the_quesition_list.size();i++){
que_view=xInflater.inflate(R.layout.quesition_layout, null);
TextView txt_que=(TextView)que_view.findViewById(R.id.txt_question_item);
//这是第三层布局要增加的地方
LinearLayout add_layout=(LinearLayout)que_view.findViewById(R.id.lly_answer);
//推断单选-多选来实现后面是*号还是*多选,
if(the_quesition_list.get(i).getType().equals("1")){
set(txt_que,the_quesition_list.get(i).getContent(),1);
}else{
set(txt_que,the_quesition_list.get(i).getContent(),0);
}
//获得答案即第三层数据
the_answer_list=the_quesition_list.get(i).getAnswers();
imglist2=new ArrayList<ImageView>();
for(int j=0;j<the_answer_list.size();j++){
ans_view=xInflater.inflate(R.layout.answer_layout, null);
TextView txt_ans=(TextView)ans_view.findViewById(R.id.txt_answer_item);
ImageView image=(ImageView)ans_view.findViewById(R.id.image);
View line_view=ans_view.findViewById(R.id.vw_line);
if(j==the_answer_list.size()-1){
//最后一条答案以下不要线是指布局的问题
line_view.setVisibility(View.GONE);
}
//推断单选多选载入不同选项图片
if(the_quesition_list.get(i).getType().equals("1")){
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.multiselect_false));
}else{
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_false));
}
Log.e("---", "------"+image);
imglist2.add(image);
txt_ans.setText(the_answer_list.get(j).getAnswer_content());
LinearLayout lly_answer_size=(LinearLayout)ans_view.findViewById(R.id.lly_answer_size);
lly_answer_size.setOnClickListener(new answerItemOnClickListener(i,j,the_answer_list,txt_ans));
add_layout.addView(ans_view);
}
/*for(int r=0; r<imglist2.size();r++){
Log.e("---", "imglist2--------"+imglist2.get(r));
}*/ imglist.add(imglist2); test_layout.addView(que_view);
}
/*for(int q=0;q<imglist.size();q++){
for(int w=0;w<imglist.get(q).size();w++){
Log.e("---", "共同拥有------"+imglist.get(q).get(w));
}
}*/ }
private void set(TextView tv_test, String content,int type) {
//为了载入问题后面的* 和*多选
// TODO Auto-generated method stub
String w;
if(type==1){
w = content+"*[多选题]";
}else{
w = content+"*";
} int start = content.length();
int end = w.length();
Spannable word = new SpannableString(w);
word.setSpan(new AbsoluteSizeSpan(25), start, end,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
word.setSpan(new StyleSpan(Typeface.BOLD), start, end,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
word.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv_test.setText(word);
}
class answerItemOnClickListener implements OnClickListener{
private int i;
private int j;
private TextView txt;
private ArrayList<Answer> the_answer_lists;
public answerItemOnClickListener(int i,int j, ArrayList<Answer> the_answer_list,TextView text){
this.i=i;
this.j=j;
this.the_answer_lists=the_answer_list;
this.txt=text; }
//实现点击选项后改变选中状态以及相应图片
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//推断当前问题是单选还是多选
/*Log.e("------", "选择了-----第"+i+"题");
for(int q=0;q<imglist.size();q++){
for(int w=0;w<imglist.get(q).size();w++){
// Log.e("---", "共同拥有------"+imglist.get(q).get(w));
}
}
Log.e("----", "点击了---"+imglist.get(i).get(j));*/ if(the_quesition_list.get(i).getType().equals("1")){
//多选
if(the_answer_lists.get(j).getAns_state()==0){
//假设未被选中
txt.setTextColor(Color.parseColor("#EA5514"));
imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.multiselect_true));
the_answer_lists.get(j).setAns_state(1);
the_quesition_list.get(i).setQue_state(1);
}else{
txt.setTextColor(Color.parseColor("#595757"));
imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.multiselect_false));
the_answer_lists.get(j).setAns_state(0);
the_quesition_list.get(i).setQue_state(1);
}
}else{
//单选 for(int z=0;z<the_answer_lists.size();z++){
the_answer_lists.get(z).setAns_state(0);
imglist.get(i).get(z).setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_false));
}
if(the_answer_lists.get(j).getAns_state()==0){
//假设当前未被选中
imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_true));
the_answer_lists.get(j).setAns_state(1);
the_quesition_list.get(i).setQue_state(1);
}else{
//假设当前已被选中
the_answer_lists.get(j).setAns_state(1);
the_quesition_list.get(i).setQue_state(1);
} }
//推断当前选项是否选中 } }
class submitOnClickListener implements OnClickListener{
private Page page;
public submitOnClickListener(Page page){
this.page=page;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//推断是否答完题
boolean isState=true;
//终于要的json数组
JSONArray jsonArray = new JSONArray();
//点击提交的时候,先推断状态,假设有未答完的就提示。假设没有再把每条答案提交(包括问卷ID 问题ID 及答案ID)
//注:不用管是否是一个问题的答案,就以答案的个数为准来提交上述格式的数据
for(int i=0;i<the_quesition_list.size();i++){
the_answer_list=the_quesition_list.get(i).getAnswers();
//推断是否有题没答完
if(the_quesition_list.get(i).getQue_state()==0){
Toast.makeText(getApplicationContext(), "您第"+(i+1)+"题没有答完", Toast.LENGTH_LONG).show();
jsonArray=null;
isState=false;
break;
}else{
for(int j=0;j<the_answer_list.size();j++){
if(the_answer_list.get(j).getAns_state()==1){
JSONObject json = new JSONObject();
try {
json.put("psychologicalId", page.getPageId());
json.put("questionId", the_quesition_list.get(i).getQuesitionId());
json.put("optionId", the_answer_list.get(j).getAnswerId());
jsonArray.put(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} }
if(isState){
if(jsonArray.length()>0){
Log.e("af", jsonArray.toString());
for(int item=0;item<jsonArray.length();item++){
JSONObject job;
try {
job = jsonArray.getJSONObject(item);
Log.e("----", "pageId--------"+job.get("pageId"));
Log.e("----", "quesitionId--------"+job.get("quesitionId"));
Log.e("----", "answerId--------"+job.get("answerId"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 遍历 jsonarray 数组。把每一个对象转成 json 对象 } } } }
} }
人不能懒惰啊,以后要多多总结。欢迎大家讨论。
android 实现调查问卷-单选-多选的更多相关文章
- Android开发技巧——自定义单选或多选的ListView
这篇其实应该是属于写自定义单选或多选的ListView的基础教程,无奈目前许多人对此的实现大多都绕了远路,反而使得这正规的写法倒显得有些技巧性了. 本文原创,转载请注明在CSDN上的出处: http: ...
- JavasScript实现调查问卷插件
原文:JavasScript实现调查问卷插件 鄙人屌丝程序猿一枚,闲来无事,想尝试攻城师是感觉,于是乎搞了点小玩意.用js实现调查问卷,实现了常规的题型,单选,多选,排序,填空,矩阵等. 遂开源贴出来 ...
- SpinnerViewPop【PopWindow样式(单选)、Dialog样式(单选+多选)的下拉菜单】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 对下拉菜单的文本区域和列表区域进行了封装.包括两种展现方式:popwindow(单选).dialog(单选+多选) 因为该封装需要在 ...
- [SAP ABAP开发技术总结]选择屏幕——按钮、单选复选框
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- "琳琅满屋"调查问卷 心得体会及结果分析
·关于心得体会 当时小组提出这个校园二手交易市场的时候,就确定了对象范围,仅仅是面向在校大学生,而且在我们之前就已经有了很多成功的商品交易的例子可以让我们去借鉴,再加上我们或多或少的有过网 ...
- 关于“Durian”调查问卷的心得体会
这周我们做了项目着手前的客户需求调查,主要以调查问卷的方式进行.其实做问卷调查并不是想象中的那么简单,首先要确定问卷调查的内容,每一个问题都要经过深思熟虑,字字斟酌,既要切合问卷主要目的,又要简洁扼要 ...
- 从Adobe调查问卷看原型设计工具大战
近年国内外原型设计工具新品频出,除了拥趸众多的老牌Axure在RP 8之后没有什么大的动作,大家都拼了命地在出新品.今天 inVision 的 Craft 出了 2.0 的预告视频,明天 Adobe ...
- 微信小程序button选中改样式-实现单选/多选
小程序实现多button单选/多选 红色为选中状态 单选 多选 ①wxss /* pages/button-select/button-select.wxss */ .button_container ...
- Scrum立会报告+燃尽图(十一月十七日总第二十五次):设计调查问卷;修复上一阶段bug
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
随机推荐
- Thinkphp图片水印和文字水印
1.Thinkphp图像处理 在TP框架中,我们经常用到图片上传,我最近写了很多关于图片上传的文章,thinkphp图片上传+validate表单验证+图片木马检测+缩略图生成等文章,今天写一下关于图 ...
- django-BBS(1)
1.首先分析BBS的设计需要,然后设计相应的数据库.填写在models.py 中 2.修改setting.py中的内容: a.将appname加入INSTALLED_APP中 b.修改DATABASE ...
- 洛谷P2280 [HNOI2003] 激光炸弹 [前缀和]
题目传送门 题目描述 输入输出格式 输入格式: 输入文件名为input.txt 输入文件的第一行为正整数n和正整数R,接下来的n行每行有3个正整数,分别表示 xi,yi ,vi . 输出格式: 输出文 ...
- ASP.NET总结——更改后
这篇重新整理的总结,我做了很久,也在草稿箱中放了很久,一直感觉没有达到和老师谈话后的水平,感觉还是需要增加一些修改,希望读者能提出宝贵意见.既这篇博客之前,我发表了一篇ASP.net的总结,在结构上, ...
- 【状压dp】Islands and Bridges
Islands and Bridges Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11034 Accepted: 2 ...
- 【贪心】Codeforces Round #436 (Div. 2) D. Make a Permutation!
题意:给你一个长度为n的数组,每个元素都在1~n之间,要你改变最少的元素,使得它变成一个1~n的排列.在保证改动最少的基础上,要求字典序最小. 预处理cnt数组,cnt[i]代表i在原序列中出现的次数 ...
- ubuntu16 安装docker
本文开发环境为Ubuntu 16.04 LTS 64位系统,通过apt的docker官方源安装最新的Docker CE(Community Edition),即Docker社区版,是开发人员和小型团队 ...
- Spring AOP动态代理
出现org.springframework.aop.framework.ProxyFactoryBean cannot be cast to 错误 在类型转换的时候, 调用getObject()方法, ...
- Problem D: 结构体:计算输入日期是该年的第几天
#include <stdio.h> struct time{ int year; int month; int day;}; int main(void) { struct time s ...
- Java高级架构师(一)第32节:Nginx的进程结构、基本配置
核心模块.事件模块.标准Http模块.可选Http模块.邮件模块.第三方模块和补丁.