一个界面实现问卷调查和评估结果

首先上图看效果:
这里写图片描述
这样的在开发中,页时常遇到.

第一步:主界面布局视图
选择器就不贴了

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#f2f2f2"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/question_index"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/question_index"
            android:gravity="center"
            android:text="1 / 5"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </LinearLayout>


    <TextView
        android:id="@+id/question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="25dp"
        android:gravity="start"
        android:text="1、您的生活各项开支(除投资外)占总收入的多少?"
        android:textColor="#434343"
        android:textSize="18sp"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="30dp">

        <RadioButton
            android:id="@+id/radiobtn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_answer_sel"
            android:button="@null"
            android:gravity="center"
            android:padding="12dp"
            android:text="低于0.5倍"
            android:textColor="#434343"
            android:textSize="14sp" />

        <RadioButton
            android:id="@+id/radiobtn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/bg_answer_sel"
            android:button="@null"
            android:gravity="center"
            android:padding="12dp"
            android:text="0.5-2倍"
            android:textColor="#434343"
            android:textSize="14sp" />

        <RadioButton
            android:id="@+id/radiobtn3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/bg_answer_sel"
            android:button="@null"
            android:gravity="center"
            android:padding="12dp"
            android:text="2倍以上"
            android:textColor="#434343"
            android:textSize="14sp" />

    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/pre_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_weight="2"
            android:background="@drawable/btn_question_pre_sel"
            android:text="上一题"
            android:textColor="#6699cc" />

        <Button
            android:id="@+id/next_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@drawable/btn_call_sel"
            android:text="下一题"
            android:textColor="#ffffff" />

    </LinearLayout>
</LinearLayout>

第二步:主界面代码

//SpareIntArray类同于HashMap,存储integer类型的值,并设置容器大小为3,
    private SparseIntArray radioArray = new SparseIntArray(3);
    private int mIndexCurrent = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化问题及答案集合
        QuestionUtils.reinitializeDatas();
        questionIndex = (TextView) findViewById(R.id.question_index);
        questionTitle = (TextView) findViewById(R.id.question);
        radiobtn1 = (RadioButton) findViewById(R.id.radiobtn1);
        radiobtn2 = (RadioButton) findViewById(R.id.radiobtn2);
        radiobtn3 = (RadioButton) findViewById(R.id.radiobtn3);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        preBtn = (Button) findViewById(R.id.pre_btn);
        nextBtn = (Button) findViewById(R.id.next_btn);
        //加载三个radioButton
        initData();
        //第一次进来,加载第一页的视图
        preQuestion();
        //设置radioGroup选择的监听
        initListener();
    }

    private void initListener() {
        //设置radioGroup的选中监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                Question question = QuestionUtils.questionList.get(mIndexCurrent);
                if(checkedId == R.id.radiobtn1) {
                    //设置当前的选中的状态
                    question.setIsSelected(true);
                    question.setIndex(0);
                }else if(checkedId == R.id.radiobtn2) {
                    question.setIsSelected(true);
                    question.setIndex(1);
                }else if(checkedId == R.id.radiobtn3) {
                    question.setIsSelected(true);
                    question.setIndex(2);
                }
            }
        });
        //设置点击事件
        preBtn.setOnClickListener(this);
        nextBtn.setOnClickListener(this);
    }

    private void preQuestion() {
        nextBtn.setText("下一题");

      //当第一题的时候,设置上一题隐藏
        if(mIndexCurrent == 0) {
            mIndexCurrent = 0;
            preBtn.setVisibility(View.GONE);
        }else {
            preBtn.setVisibility(View.VISIBLE);
            mIndexCurrent--;
        }
        Question question = QuestionUtils.questionList.get(mIndexCurrent);
        //刷新视图
        refreshQuestionView(mIndexCurrent,question);
    }

    //引导条标题及内容的变化
    private void refreshQuestionView(int indexCurrent, Question question) {
        questionIndex.setText((indexCurrent+1)+"/"+QuestionUtils.questionList.size());
        questionTitle.setText(question.getTitle());
        radiobtn1.setText(question.getAnswerArray()[0]);
        radiobtn2.setText(question.getAnswerArray()[1]);
        radiobtn3.setText(question.getAnswerArray()[2]);

        //设置选中的btn
        if(question.isSelected()) {
          int checkID = radioArray.get(question.getIndex());
            //选中
            radioGroup.check(checkID);
        }else {
            //清除选中
            radioGroup.clearCheck();
        }
    }

    private void initData() {
        radioArray.append(0,R.id.radiobtn1);
        radioArray.append(1,R.id.radiobtn2);
        radioArray.append(2,R.id.radiobtn3);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pre_btn :
                //点击上一题
                preQuestion();
                break;
            case R.id.next_btn :
                //点击下一题
                Question question = QuestionUtils.questionList.get(mIndexCurrent);
               //每次点击的时候,先清除选中的radioButton
                radioGroup.clearCheck();
                if(question.isSelected()) {
                    nextQuestion();
                }else {
                    Toast.makeText(this,"还未选中问题答案",Toast.LENGTH_SHORT).show();;
                    return;
                }
                break;
        }
    }

    private void nextQuestion() {
        preBtn.setVisibility(View.VISIBLE);
        //因为每次都需要点击后,才会走此方法,所以最后一次需要减2
        if(mIndexCurrent >= QuestionUtils.questionList.size() -2) { //到最后一题了
            mIndexCurrent = QuestionUtils.questionList.size() -1;
            nextBtn.setText("提交");
        }else {
            nextBtn.setText("下一题");
            mIndexCurrent++;
        }
        Question question = QuestionUtils.questionList.get(mIndexCurrent);
        //刷新视图
        refreshQuestionView(mIndexCurrent,question);
    }

第三步:

/**
 * 问题及标题资源文件集合
 */
public class QuestionUtils {
    //问题标题集合
    public static int[] questionArray = new int[]{R.string.question1,
            R.string.question2, R.string.question3, R.string.question4, R.string.question5};

    //答案选项集合
    public static int[] answerArray = new int[]{R.array.question1,
            R.array.question2, R.array.question3, R.array.question4, R.array.question5};

    //每题的集合对象(包括问题标题和答案)
    public static final List<Question> questionList = new ArrayList<Question>(questionArray.length);
    static {
        for (int i = 0; i < questionArray.length; i++) {
            questionList.add(new Question(getString(questionArray[i]), getStrArray(answerArray[i])));
        }
    }

    private QuestionUtils() {
    }

    public static void reinitializeDatas() {
        for (int i = 0; i < questionList.size(); i++) {
            questionList.get(i).setIsSelected(false);
            questionList.get(i).setIndex(-1);
        }
    }

    private static String getString(int resId) {
        // MyApplication.getInstance() 返回的是当前应用的app对象即可以理解为context
        return MyApplication.getInstance().getResources().getString(resId);
    }

    private static String[] getStrArray(int arrayResId) {
        return MyApplication.getInstance().getResources().getStringArray(arrayResId);
    }
}

**第四步:**Question对象集合

public class Question {
    private String title;
    private String[] answerArray;

    //是否选中
    private boolean isSelected = false;
    private int index = -1;

    public Question(String title, String[] answerArray) {
        this.title = title;
        this.answerArray = answerArray;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String[] getAnswerArray() {
        return answerArray;
    }

    public void setAnswerArray(String[] answerArray) {
        this.answerArray = answerArray;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setIsSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}

**第五步:**array里面的资源文件
在values中新建array.xml资源文件

<string-array name="question1">
        <item>0</item>
        <item>0-5%</item>
        <item>5-10%</item>
    </string-array>
    <string-array name="question2">
        <item>低于0.5倍</item>
        <item>0.5-2倍</item>
        <item>2倍以上</item>
    </string-array>
    <string-array name="question3">
        <item>完全没做过投资</item>
        <item>一般水平</item>
        <item>非常有经验</item>
    </string-array>
    <string-array name="question4">
        <item>资产保值,我不愿意承担任何风险</item>
        <item>尽可能保证本金安全,不在乎收益率比较低</item>
        <item>产生较多的收益,可以承担一定的投资风险</item>
    </string-array>
    <string-array name="question5">
        <item>0</item>
        <item>0-5%</item>
        <item>5-10%</item>
    </string-array>

以上就可实现.

小常识:在新建了Application之后,必须要在AndroidMainfest.xml中取申明注册,不然会报空指针异常

<application
        android:name=".MyApplication"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值