问卷调查Demo

效果图.gif



    

        
    

    

        

        

            

                

                

            

            

                

                

            


            

                

                

            

            

                

                
            
        

        

            

            

        
    

    
/**
 * Created by zhangyapeng on 2018/10/26.
 */
class SurveyBean {
    private String title;
    private String[] sel;

    public SurveyBean(String title, String[] sel) {
        this.title = title;
        this.sel = sel;
    }

    public String getTitle() {
        return title;
    }

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

    public String[] getSel() {
        return sel;
    }

    public void setSel(String[] sel) {
        this.sel = sel;
    }
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.rl_survey)
    RelativeLayout mRlSurvey;
    @BindView(R.id.title)
    TextView       mTitle;
    @BindView(R.id.check_image1)
    ImageView      mCheckImage1;
    @BindView(R.id.check_text1)
    TextView       mCheckText1;
    @BindView(R.id.check_item1)
    LinearLayout   mCheckItem1;
    @BindView(R.id.check_image2)
    ImageView      mCheckImage2;
    @BindView(R.id.check_text2)
    TextView       mCheckText2;
    @BindView(R.id.check_item2)
    LinearLayout   mCheckItem2;
    @BindView(R.id.check_image3)
    ImageView      mCheckImage3;
    @BindView(R.id.check_text3)
    TextView       mCheckText3;
    @BindView(R.id.check_item3)
    LinearLayout   mCheckItem3;
    @BindView(R.id.check_image4)
    ImageView      mCheckImage4;
    @BindView(R.id.check_text4)
    TextView       mCheckText4;
    @BindView(R.id.check_item4)
    LinearLayout   mCheckItem4;
    @BindView(R.id.pre)
    TextView       mPre;
    @BindView(R.id.tv_page)
    TextView       mTvPage;
    @BindView(R.id.submit)
    Button         mSubmit;


    
//     设置初始位置,获取第一条数据
    public static int possition = 0;

//    存储数据的集合
    private List beanAar = new ArrayList<>();

//    记录分数
    private int[] checkPossition = new int[]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};

//    4张图片
    private ImageView[] mImageViews;

    private Handler mHandler = new Handler();

//    计算总分数
    private int score;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        mImageViews = new ImageView[]{mCheckImage1, mCheckImage2, mCheckImage3, mCheckImage4};

        mCheckItem1.setOnClickListener(this);
        mCheckItem2.setOnClickListener(this);
        mCheckItem3.setOnClickListener(this);
        mCheckItem4.setOnClickListener(this);
        mPre.setOnClickListener(this);
        mSubmit.setOnClickListener(this);


        possition = 0;
        init();
        setDatas();

    }

    private void setDatas() {
        mTitle.setText(beanAar.get(possition).getTitle());
        mCheckText1.setText(beanAar.get(possition).getSel()[0]);
        mCheckText2.setText(beanAar.get(possition).getSel()[1]);
        mCheckText3.setText(beanAar.get(possition).getSel()[2]);
        mCheckText4.setText(beanAar.get(possition).getSel()[3]);

        mTvPage.setText(possition + 1 + "/10");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.check_item1:
                checkPossition[possition] = 0;
                hide(0);
                next();

                break;
            case R.id.check_item2:
                checkPossition[possition] = 1;
                hide(1);
                next();

                break;
            case R.id.check_item3:
                checkPossition[possition] = 2;
                hide(2);
                next();

                break;
            case R.id.check_item4:
                checkPossition[possition] = 3;
                hide(3);
                next();

                break;

            case R.id.pre:
                pre();
                break;

            case R.id.submit:
                Log.v("666", score+"");
                Toast.makeText(MainActivity.this, "score" + score, Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    private void next() {
        possition += 1;

        if (possition > 9) {
            possition = 9;
            addScore();
            mSubmit.setVisibility(View.VISIBLE);

            return;
        }

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                hide();
                setDatas();
            }
        }, 300);
    }

    /**
     * 上一步
     */
    private void pre() {
        possition -= 1;
        if (possition < 0) {
            possition = 0;
        }
        if (possition < 9) {
            mSubmit.setVisibility(View.INVISIBLE);
        }
        hide(checkPossition[possition]);
        setDatas();
    }

    private void addScore() {
        for (int i = 0; i < checkPossition.length; i++) {
            if (checkPossition[i] == -1) {
                score = 0;
                return;
            }
        }

        for (int i = 0; i < checkPossition.length; i++) {
            switch (checkPossition[i]) {
                case 0:
                    score += 10;
                    break;
                case 1:
                    score += 8;
                    break;
                case 2:
                    score += 6;
                    break;
                case 3:
                    score += 3;
                    break;

                default:
                    break;
            }
        }
    }

    private void hide(int i) {
        for (int i1 = 0; i1 < mImageViews.length; i1++) {
            if (i1 == i) {
                mImageViews[i1].setImageResource(R.drawable.button_orange_1);
            } else {
                mImageViews[i1].setImageResource(R.drawable.button_white);
            }
        }
    }

    private void hide() {
        for (int i1 = 0; i1 < mImageViews.length; i1++) {
            mImageViews[i1].setImageResource(R.drawable.button_white);
        }
    }

    public void init() {
        beanAar.add(new SurveyBean("1、您的年龄:", new String[]{"18--30岁", "31-45岁", "46-55岁", "55岁以上"}));
        beanAar.add(new SurveyBean("2、您的身体健康状况:", new String[]{"非常好", "好", "一般", "差"}));
        beanAar.add(new SurveyBean("3、您的投资年限", new String[]{"10年以上", "5-10年", "1-5年", "1年内"}));
        beanAar.add(new SurveyBean("4、您的投资经验可以被概括为:", new String[]{"丰富:是一位积极和有经验的证券投资者,并倾向于自己作出投资决定", "一般:具有一定的证券投资经验,需要进一步的指导", "有限:有过购买国债,货币型基金等保本型金融产品投资经验", "无:除银行活期和投定期储蓄存款外,基本没有其他资经验"}));
        beanAar.add(new SurveyBean("5、您曾经或正在做的投资产品(若有多项请选风险最大的一项):", new String[]{"期货、权证", "股票", "债券、基金", "无"}));
        beanAar.add(new SurveyBean("6、今后5年内您的预期收入:", new String[]{"预期收入将逐渐增加", "预期收入将保持稳定", "预期收入将不断减少", "无"}));
        beanAar.add(new SurveyBean("7、以下哪项描述最符合您的投资态度?", new String[]{"希望赚取高回报,愿意为此承担较大本金损失", "寻求资金的较高收益和成长性,愿意为此承担有限本金损失", "保守投资,不希望本金损失,愿意承担一定幅度的收益波动", "厌恶风险,不希望本金损失,希望获得稳定回报"}));
        beanAar.add(new SurveyBean("8、您用于投资的资金在您的总资产中占比大致是多少(除自用和经营性财产外)?", new String[]{"大于50%", "30%~50%", "10%~30%", "小于10%"}));
        beanAar.add(new SurveyBean("9、您在投资中能够接受的最大本金损失大致是多少?", new String[]{"最大本金亏损50%以上", "最大本金亏损20%~50%", "最大本金亏损5%~20%", "最大本金亏损5%以内"}));
        beanAar.add(new SurveyBean("10.您的投资目的是?", new String[]{"关心长期的高回报,能够接受短期的资产价值波动", "倾向长期的成长,较少关心短期的回报以及波动", "希望投资能获得一定的增值,同时获得波动适度的年回报", "只想确保资产的安全性,同时希望能够得到固定的收益"}));
    }
}
button_orange_1.png
button_white.png

survey_commit.jpg

你可能感兴趣的:(问卷调查Demo)