一个简单的问答app

目标:一共有三个问题,TextView能够显示问题的内容,下面的两个按钮是自己认为正确或者错误,用户按下按钮之后,系统能够显示用户的答案是正确的还是错误的。

点击下一个问题按钮,就可以显示下一个问题,轮流循环

步骤:

1.在之前,我们已经把一个TextView和两个Button(correct和incorrect)设计好了,我们在界面上还需要设计一个Next按钮

<Button
    android:id="@+id/next_question"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next_question"
    ></Button>

在activity_main.xml中把按钮设计好,把id和text都加上。

界面设计好了。

2.下面我们开始写逻辑。

首先我们需要有几个问题,新建一个问题类,类中有两个属性,分别是id和对错(用boolean表示)

package com.example.helloworld;

public class question {
    private int mQuestionId;
    private boolean mQuestionAnswer;
    public question(int id,boolean answer){
        this.mQuestionAnswer=answer;
        this.mQuestionId=id;
    }
    public int getmQuestionId() {
        return mQuestionId;
    }
    public void setmQuestionId(int mQuestionId) {
        this.mQuestionId = mQuestionId;
    }
    public boolean getmQuestionAnswer() {
        return mQuestionAnswer;
    }
    public void setmQuestionAnswer(boolean mQuestionAnswer) {
        this.mQuestionAnswer = mQuestionAnswer;
    }
   
}

好了,问题也设计好了,下面我们开始正式开始写。

在资源的strings.xml中将问题设计好

<string name="question_zhoujie">zhoujie\'s age is 22</string>
   <string name="question_zhangwei">zhangwei\'s age is 24</string>
   <string name="question_majian">majian\'s age is 26</string>

需要用一个数组来存储问题,

//set the question array
private question[] questions=new question[]{
    new question(R.string.question_zhoujie, false),
    new question(R.string.question_majian, true),
    new question(R.string.question_zhangwei, false),
};

我们需要得到页面的所有元素,三个按钮和一个TextView

private Button mtrueButton;
    private Button mFalseButton;
    private Button mNext;
    private TextView mTextView;

还需要用一个索引值来存储当前数组的位置,默认为0

//the index of the questions array,default 0
    private int mCurrentIndex;

当app启动时,便显示第一个问题

//show the question on the textview,use findViewById
       mTextView=(TextView) findViewById(R.id.question_text_view);
       //get current question string
       int questionid=questions[mCurrentIndex].getmQuestionId();
       //use resource id and show the text
       mTextView.setText(questionid);

按下按钮时,便显示下一个问题,我们需要设置一个监听器,每按一下按钮都能显示下一个问题。需要做的功能便是:

索引值+1,并且能够循环,这里使用取余来实现。并且能够更新TextView的内容

//get the next_question button
       mNext=(Button) findViewById(R.id.next_question);
       mNext.setOnClickListener(new View.OnClickListener() {
          
           @Override
           public void onClick(View v) {
               //add index and update the content of the textview
               mCurrentIndex=(mCurrentIndex+1)%questions.length;
               int questionid=questions[mCurrentIndex].getmQuestionId();
               mTextView.setText(questionid);
           }
       });

 

next已经设置好了。

下面我们需要设置,两个答案的按钮,这里我们使用一个check函数来实现,当答案和预定的一样时,告诉用户答案是对的,不一样时,告诉用户答案是错误的

private void check(boolean userPush){
       //get the correct answer
       boolean answer=questions[mCurrentIndex].getmQuestionAnswer();
       if(answer==userPush){
           Toast.makeText(MainActivity.this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
       }else{
           Toast.makeText(MainActivity.this,R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
       }
   }
 

设置监听器,监听器中的函数便是check函数

mtrueButton.setOnClickListener(new View.OnClickListener() {
          
           @Override
           public void onClick(View v) {
               //show correct toast
               check(true);
           }
       });
       mFalseButton=(Button) findViewById(R.id.false_button);
       //set a onclickListener for the falser button
       mFalseButton.setOnClickListener(new View.OnClickListener() {
          
           @Override
           public void onClick(View v) {
               //show incorrect toast
               check(false);
           }
       });

好了,暂时一个简陋的但是功能完善的app写完了,我们来运行一下。

imageimageimage

ok,演示结束。这个功能暂时告一段落

转载于:https://my.oschina.net/u/2275839/blog/379852

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值