问卷调查的布局

大概效果如图

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


<?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;
  private View que_view;
  private View ans_view;
  private LayoutInflater xInflater;
  private Page page;
  private ArrayList<ArrayList<ImageView>> imglist=new ArrayList<ArrayList<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("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;
      JSONArray jsonArray = new JSONArray();

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

posted on 2017-06-18 08:38  xiaoyun1  阅读(181)  评论(0编辑  收藏  举报