Android课堂练习一:世界杯问卷之页面跳转与重置按钮

项目名称:世界杯问卷

今天在课堂上,老师布置了一个Android项目练习,实现一个世界杯问卷。

要求:

  • 用单选框和复选框实现
  • 然后将选择提交给另一个页面

编译环境:Android studio 3.1.2;
SDK Lever 19,Revision 4.4;
java version 1.8.0_171;

遇到的问题:

  • 如何实现重置功能
  • 如何实现页面跳转及数据传递功能

解决方案

  • 重置功能:使用Android传递参数组件Intent跳转回原页面
  • 页面跳转:应用用Android传递参数组件Intent在Activity页面之间传递数据(未实现,因为懒得补上去

页面

  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/q1"
        android:text="@string/q1text"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <RadioGroup android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rb_1"
            android:text="@string/rb1text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <RadioButton
            android:id="@+id/rb_2"
            android:text="@string/rb2text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <RadioButton
            android:id="@+id/rb_3"
            android:text="@string/rb3text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <RadioButton
            android:id="@+id/rb_4"
            android:text="@string/rb4text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>
    <TextView
        android:id="@+id/q2"
        android:text="@string/q2text"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <GridLayout
        android:columnCount="2"
        android:rowCount="2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/cb_1"
            android:text="@string/cb1text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/cb_2"
            android:text="@string/cb2text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/cb_3"
            android:text="@string/cb3text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/cb_4"
            android:text="@string/cb4text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </GridLayout>
    <LinearLayout
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/b1text"
            android:textSize="20dp" />
        <Button
            android:id="@+id/btn_2"
            android:text="@string/b2text"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        <TextView
            android:id="@+id/ans_text1"
            android:text="@string/ans_txt1"
            android:textSize="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/ans_text2"
            android:text="@string/ans_txt2"
            android:textSize="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/ans_text3"
            android:text="@string/ans_txt3"
            android:textSize="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/ans_text4"
            android:text="@string/ans_txt4"
            android:textSize="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

配置文件

  • strings.xml
<resources>
    <string name="app_name">test1</string>
    <string name="q1text">球王"贝利"是哪个国家的人:</string>
    <string name="q2text">下列足球队中,哪些队曾获得过世界杯冠军:</string>
    <string name="b1text">提交</string>
    <string name="b2text">重置</string>
    <string name="rb1text">巴西</string>
    <string name="rb2text">德国</string>
    <string name="rb3text">美国</string>
    <string name="rb4text">法国</string>
    <string name="cb1text">法国国家队</string>
    <string name="cb2text">中国国家队</string>
    <string name="cb3text">巴西国家队</string>
    <string name="cb4text">美国国家队</string>
    <string name="ans_txt1"> </string>
    <string name="ans_txt2"> </string>
    <string name="ans_txt3"> </string>
    <string name="ans_txt4"> </string>
</resources>

主控文件

  • MainActivity.java
package com.example.test1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
    Button b1,b2;
    CheckBox ch1,ch2,ch3,ch4;
    RadioButton rb1,rb2,rb3,rb4;
    TextView txt1,txt2,txt3,txt4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView(){
        b1 = (Button)findViewById(R.id.btn_1);
        b2 = (Button)findViewById(R.id.btn_2);
        ch1 = (CheckBox)findViewById(R.id.cb_1);
        ch2 = (CheckBox)findViewById(R.id.cb_2);
        ch3 = (CheckBox)findViewById(R.id.cb_3);
        ch4 = (CheckBox)findViewById(R.id.cb_4);
        rb1 = (RadioButton)findViewById(R.id.rb_1);
        rb2 = (RadioButton)findViewById(R.id.rb_2);
        rb3 = (RadioButton)findViewById(R.id.rb_3);
        rb4 = (RadioButton)findViewById(R.id.rb_4);
        txt1 = (TextView)findViewById(R.id.ans_text1);
        txt2 = (TextView)findViewById(R.id.ans_text2);
        txt3 = (TextView)findViewById(R.id.ans_text3);
        txt4 = (TextView)findViewById(R.id.ans_text4);
        b1.setOnClickListener(new mClick());
        b2.setOnClickListener(new mClick());
    }
    class mClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            String t1,t2,t3,t4;
            Answer a;
            if (v==b1){
                a = submit();
                t1="您的得分:"+a.getScore();
                t2=a.getAns1();
                t3=a.getAns2();
                t4 = "您提交的隐藏信息:喜欢世界杯!";
                txt1.setText(t1);
                txt2.setText(t2);
                txt3.setText(t3);
                txt4.setText(t4);
            }else if (v==b2){
                reset();
            }
        }
    }
    private void reset(){
        Intent intent = new Intent(MainActivity.this,MainActivity.class);
        startActivity(intent);
    }
    private Answer submit(){
        Answer a= new Answer();
        int  score = 0,i=0;
        String ans1 = "您提交的答案一:",ans2 = "您提交的答案二:";
        /*单选*/
        if (rb1.isChecked()){
            score+=1;
            ans1+=" "+rb1.getText();
        }else if (rb2.isChecked()){
            ans1+=" "+rb2.getText();
        }else if (rb3.isChecked()){
            ans1+=" "+rb3.getText();
        }else if (rb4.isChecked()){
            ans1+=" "+rb4.getText();
        }
        /*复选*/
        if (ch1.isChecked()){
            i+=1;
            ans2+=" "+ch1.getText();
        }
        if (ch2.isChecked()){
            i-=1;
            ans2+=" "+ch2.getText();
        }
        if (ch3.isChecked()){
            i+=1;
            ans2+=" "+ch3.getText();
        }
        if (ch4.isChecked()){
            i-=1;
            ans2+=" "+ch4.getText();
        }
        if (i==2){
            score+=1;
        }
        a.set(score,ans1,ans2);
        return a;
    }
}

class Answer{
    private int  score;
    private String ans1,ans2;
    Answer(){
        score = 0;
        ans1 = "";
        ans2 = "";
    }
    Answer(int a,String b,String c){
        score = a;
        ans1 = b;
        ans2 = c;
    }
    public int getScore(){
        return score;
    }
    public String getAns1(){
        return ans1;
    }
    public String getAns2(){
        return ans2;
    }
    public void setAns2(String a){
        ans2 = a;
    }
    public void setAns1(String a){
        ans1 = a;
    }
    public void setAns2(int a){
        score = a;
    }
    public void set(int a,String b,String c){
        score = a;
        ans1 = b;
        ans2 = c;
    }
}

运行图

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值