您的位置:首页 > 移动开发 > Android开发

Android开发:实现简单的问卷调查

2019-06-16 18:15 183 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_42665285/article/details/92415028

Android开发:实现简单的问卷调查




1.activity_main.xml
主界面整体是由ScrollView控件控制,ScrollView它继承自FrameLayout,所以它是一种特殊类型的FrameLayout,因为它可以使用用户滚动显示一个占据的空间大于物理显示的视图列表。

<?xml version="1.0" encoding="utf-8"?>
<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="@mipmap/back1">

<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="#EAF0FF"
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/whitecolor"

/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="调查问卷"
android:textSize="28dp"
android:textColor="@android:color/black"
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="20dp"
android:textColor="@android:color/black"
android:layout_gravity="center"
android:gravity="center"
android:background="#00FF00"/>
</LinearLayout>
</LinearLayout>

</ScrollView>

2.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="12dp"
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>

3.question_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="14dp"
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="#FFFFFF"
>

</LinearLayout>

</LinearLayout>

4.MainActivity.java
注意里面的i j z等角标很容易弄糊涂,需要十分小心,将每个答案拼好成一个json数据。

package com.bignerdranch.android.myapplication;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.util.Size;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private LinearLayout test_layout;
private Page the_page;
//答案列表
private ArrayList<Answer> the_answer_list;
//问题列表
private ArrayList<Question> the_question_list;
//问题所在的View
private View que_view;
//答案所在的View
private View ans_view;
private Layou
7ff7
tInflater 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() {

Answer a_one=new Answer();
a_one.setAnswerId("00");
a_one.setAnswer_content("男");
a_one.setAns_state(0);
Answer a_two=new Answer();
a_two.setAnswerId("11");
a_two.setAnswer_content("女");
a_two.setAns_state(0);

Answer a_b=new Answer();
a_b.setAnswerId("0");
a_b.setAnswer_content("容易or根本无");
a_b.setAns_state(0);

Answer a_c=new Answer();
a_c.setAnswerId("1");
a_c.setAnswer_content("很少or有一点");
a_c.setAns_state(0);

Answer a_d=new Answer();
a_d.setAnswerId("2");
a_d.setAnswer_content("有时or有些");
a_d.setAns_state(0);

Answer a_e=new Answer();
a_e.setAnswerId("3");
a_e.setAnswer_content("经常or相当");
a_e.setAns_state(0);

Answer a_f=new Answer();
a_f.setAnswerId("4");
a_f.setAnswer_content("总是or非常");
a_f.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_b);
answers_two.add(a_c);
answers_two.add(a_d);
answers_two.add(a_e);
answers_two.add(a_f);

Question q_one=new Question();
q_one.setQuestionId("00");
q_one.setType("0");
q_one.setContent("1、您的性别?");
q_one.setAnswers(answers_one);
q_one.setQue_state(0);

Question q_two=new Question();
q_two.setQuestionId("01");
q_two.setType("100");//type=1,为多选题,不能把type设置为1
q_two.setContent("2、您体力充沛吗?");
q_two.setAnswers(answers_two);
q_two.setQue_state(0);

Question q_three=new Question();
q_three.setQuestionId("02");
q_three.setType("2");
q_three.setContent("3、您容易疲劳吗?");
q_three.setAnswers(answers_two);
q_three.setQue_state(0);

Question q_four=new Question();
q_four.setQuestionId("03");
q_four.setType("3");
q_four.setContent("4、您说话声音低弱无力吗吗?");
q_four.setAnswers(answers_two);
q_four.setQue_state(0);

Question q_five=new Question();
q_five.setQuestionId("04");
q_five.setType("4");
q_five.setContent("5、您感觉胸闷不乐、情绪低沉吗?");
q_five.setAnswers(answers_two);
q_five.setQue_state(0);

Question q_six=new Question();
q_six.setQuestionId("05");
q_six.setType("5");
q_six.setContent("6、您比一般人耐受不了寒冷(冬天是寒冷,夏天是冷空调、电扇等)吗?");
q_six.setAnswers(answers_two);
q_six.setQue_state(0);

ArrayList<Question> questions=new ArrayList<Question>();
questions.add(q_one);
questions.add(q_two);
questions.add(q_three);
questions.add(q_four);
questions.add(q_five);
questions.add(q_six);

page=new Page();
page.setPageId("000");
page.setStatus("0");
page.setTitle("饮食调查表");
page.setQuestions(questions);
//加载布局
initView(page);
}
private void initView(Page page) {

test_layout=(LinearLayout)findViewById(R.id.lly_test);
TextView page_txt=(TextView)findViewById(R.id.txt_title);
page_txt.setText(page.getTitle());

the_question_list=page.getQuestions();

for(int i=0;i<the_question_list.size();i++){
que_view=xInflater.inflate(R.layout.question_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_question_list.get(i).getType().equals("1")){
set(txt_que,the_question_list.get(i).getContent(),1);
}else{
set(txt_que,the_question_list.get(i).getContent(),0);
}

the_answer_list=the_question_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_question_list.get(i).getType().equals("1")){
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.whitecolor));
}else{
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.whitecolor));
}
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) {

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) {

if(the_question_list.get(i).getType().equals("1")){
//多选
if(the_answer_lists.get(j).getAns_state()==0){
//如果未被选中
//txt.setTextSize(22);
txt.setTextColor(Color.parseColor("#EA5514"));
imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.whitecolor));
the_answer_lists.get(j).setAns_state(1);
the_question_list.get(i).setQue_state(1);
}else{
//txt.setTextSize(22);
txt.setTextColor(Color.parseColor("#595757"));
imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.whitecolor));
the_answer_lists.get(j).setAns_state(0);
the_question_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.whitecolor));
//}
if(the_answer_lists.get(j).getAns_state()==0){
//如果当前未被选中
//txt.setTextSize(22);
txt.setTextColor(Color.parseColor("#EA5514"));
imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.whitecolor));
the_answer_lists.get(j).setAns_state(1);
the_question_list.get(i).setQue_state(1);
}else{
//如果当前已被选中
//txt.setTextSize(22);
txt.setTextColor(Color.parseColor("#595757"));
imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.whitecolor));
the_answer_lists.get(j).setAns_state(0);
the_question_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_question_list.size();i++){
the_answer_list=the_question_list.get(i).getAnswers();
//判断是否有题没答完
if(the_question_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_question_list.get(i).getQuestionId());
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("----", "questionId--------"+job.get("questionId"));
Log.e("----", "answerId--------"+job.get("answerId"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  // 遍历 jsonarray 数组,把每一个对象转成 json 对象

}

}

}

}
}

}

5.Page.java

package com.bignerdranch.android.myapplication;

import java.util.ArrayList;

public class Page {
//问卷id
private String pageId;

private String status;

private String title;
//题目
private ArrayList<Question> questions;

public ArrayList<Question> getQuestions() {
return questions;
}
public void setQuestions(ArrayList<Question> questions) {
this.questions = questions;
}

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;
}

}

6.Question.java

package com.bignerdranch.android.myapplication;

import java.util.ArrayList;

public class Question {

private String questionId;

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 getQuestionId() {
return questionId;
}
public void setQuestionId(String questionId) {
this.questionId = questionId;
}
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;
}

}

7.Answer.java

package com.bignerdranch.android.myapplication;

public class Answer {

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;
}

}

8.参考博客
https://www.geek-share.com/detail/2713120444.html
https://www.geek-share.com/detail/2646112610.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐