博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 实现调查问卷-单选-多选
阅读量:5733 次
发布时间:2019-06-18

本文共 7992 字,大约阅读时间需要 26 分钟。

非常久没写东西了。今天来总结下有关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
quesitions; public ArrayList
getQuesitions() { return quesitions; } public void setQuesitions(ArrayList
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
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
getAnswers() { return answers; } public void setAnswers(ArrayList
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

id为lly_test的布局就是终于要增加的目的布局

然后是quesition_layout.xml

> <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
the_answer_list; //问题列表 private ArrayList
the_quesition_list; //问题所在的View private View que_view; //答案所在的View private View ans_view; private LayoutInflater xInflater; private Page page; //以下这两个list是为了实现点击的时候改变图片。由于单选多选时情况不一样。为了方便控制 //存每一个问题下的imageview private ArrayList
> imglist=new ArrayList
>(); //存每一个答案的imageview private ArrayList
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
answers_one=new ArrayList
(); answers_one.add(a_one); answers_one.add(a_two); ArrayList
answers_two=new ArrayList
(); answers_two.add(a_one); answers_two.add(a_two); answers_two.add(a_three); answers_two.add(a_four); ArrayList
answers_three=new ArrayList
(); 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
quesitions=new ArrayList
(); 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
(); for(int j=0;j
the_answer_lists; public answerItemOnClickListener(int i,int j, ArrayList
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
0){ Log.e("af", jsonArray.toString()); for(int item=0;item
人不能懒惰啊,以后要多多总结。欢迎大家讨论。

你可能感兴趣的文章
NYOJ32:组合数(DFS入门)
查看>>
使用Callable和Future接口创建线程
查看>>
BZOJ 2568 比特集合
查看>>
sql语句返回主键SCOPE_IDENTITY()
查看>>
MongoDB培训
查看>>
机器学习开源项目精选TOP30
查看>>
一起谈.NET技术,ASP.NET缓存全解析6:数据库缓存依赖
查看>>
代码分析系列 内存执行过程
查看>>
iOS开发-邮件发送
查看>>
/etc/resolv.conf文件详解
查看>>
【转】VC的MFC中重绘函数的使用总结(整理)
查看>>
JQuery日记_5.13 Sizzle选择器(六)选择器的效率
查看>>
System.gc()与Object.finalize()的区别
查看>>
Memcache存储大数据的问题
查看>>
HTML5区域范围文本框实例页面
查看>>
oracle查看经常使用的系统信息
查看>>
技术工坊|如何利用ERC875协议开发世界杯区块链门票?(北京)
查看>>
Django_4_视图
查看>>
Linux的netstat命令使用
查看>>
shell实例100例《五》
查看>>