Android编程权威指南-----(二)Android与MVC设计模式

前半部分主要讲了MVC 分别是 模型 视图 控制器
那么现在讲之前写的在增加题目。那么就需要了模型来存储题目。
创建一个类。Question 。定义其属性,int mTextResId boolean mAnswerTrue id 是int的。引用的时候是R.id 返回值是int

然后配置Android Studio识别成员变量的m前缀。
File->Setting->Editor->Cody Style->java->Code Generation
fiedls写m
Static 写s
这里写图片描述

然后像eclipse那样自动创建get set方法。

控制层就是 QuizAcivtity
模型就是 Question
视图是 activity_quiz.xml

然后将之前在text写的string删除。不是固定的。
在QuizAcivtity中定义个Question的数组。并定义值。注意,这个数组的每个值都是对象Question,而且每个Question的参数是R.string.对应的题目的。和true,false的

再定义个下标变量。

像之前的Butoon按钮一样。在布局中添加按钮。还要在strings.xml中定义string。
通过R获取next_button 然后设置监听器。监听到按下就使下标变量加1.
注意 不要让其增加超过整个数组的长度,那么就+1然后%整个数组的长度。
获取text对象就是 TextViex 然后通过R.id获取其对象。当然在activity.xml中对应的text定义了id属性
在onCreate方法中获取。

 mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

每次按钮变化要更新其text。还有要注意最开始也要有。下标初始是0,text显示就是第一个。

对其进行判断对错。单独写个方法,让其与这个Question的对错与按钮按下的是什么比较,对应弹出什么提示。在TRUEButton和FALSEButton监听器下都要进行调用。

添加back按钮。同样类似。注意。如果是第一个的话。应该判断是否是第一个,不是的话才进行back事件。

QuizActivity代码

package com.example.administrator.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class QuizActivity extends AppCompatActivity {
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;
    private Button mBackButton ;
    private  Question[] mQuestionBank = new Question[]{
            new Question(R.string.question_oceans,true),
            new Question(R.string.question_mideast,false),
            new Question(R.string.question_africa,false),
            new Question(R.string.question_americas,true),
            new Question(R.string.question_asia,true)
    };

        private int mCurrentIndex = 0;

    //将重复的代码提取出来
    private void updateQuestion(){
        int question= mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
    }

    //判断对错的方法
    private void checkAnswer(boolean userPressedTrue){
        //先获取当前的对错
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId =0;
        if(userPressedTrue==answerIsTrue){
            messageResId = R.string.correct_toast;
        }else {
            messageResId = R.string.incorrect_toast;
        }
        Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        //获取那个文本textView对象,然后在在数组中获取其id,即问题的id string资源,在set写入。
        //这样只是第一个就是 mCurrent初始定义为0
        mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

        //获取button
        mTrueButton = (Button) findViewById(R.id.true_button);
        mFalseButton = (Button) findViewById(R.id.false_button);
        //设置监听器
        mTrueButton.setOnClickListener( new View.OnClickListener(){
            @Override
            public void onClick(View v){
                checkAnswer(true);
            }

        });
//

        mFalseButton.setOnClickListener( new View.OnClickListener(){
            @Override
            public void onClick(View v){
                checkAnswer(false);
            }

        });

        //设置其新增的NEXT按钮
        mNextButton = (Button) findViewById(R.id.next_button);
        mNextButton.setOnClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v){
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length;
                updateQuestion();
            }

        });
        //设置text的按下监听
        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length;
                updateQuestion();
            }
        });

        //获取BACK返回上一个的按钮
        mBackButton = (Button) findViewById(R.id.back_button);
        mBackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int tishi ;
                tishi = R.string.tishi;
                if (mCurrentIndex!=0){
                    mCurrentIndex = (mCurrentIndex-1) % mQuestionBank.length;
                    updateQuestion();
                }

            }
        });

        //这个是什么都没有按的时候也要有,就是当前的。比如开始的时候。
        updateQuestion();

    }
}

Question

package com.example.administrator.geoquiz;


public class Question {
    private int mTextResId;
    private boolean mAnswerTrue;

    public Question(int textResId,boolean answerTrue){
        mTextResId = textResId;
        mAnswerTrue = answerTrue;
    }

    public int getTextResId() {
        return mTextResId;
    }

    public void setTextResId(int textResId) {
        mTextResId = textResId;
    }

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

    public void setAnswerTrue(boolean answerTrue) {
        mAnswerTrue = answerTrue;
    }
}

activity_quiz.xml

<?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="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:id="@+id/question_text_view"
        />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/true_button"
        android:id="@+id/true_button"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button"
        android:id="@+id/false_button"
        />

</LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

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

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

string.xml

<resources>
    <string name="true_button">TRUE</string>
    <string name="false_button">FALSE</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
    <string name="app_name">test1</string>
    <string name="next_button">NEXT</string>
    <string name="back_button">BACK</string>
    <string name="tishi">This is first</string>
    <string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
    <string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
    <string name="question_africa">The source of the Nile River is in Egypt.</string>
    <string name="question_americas">The Amazon River is the longests river in the Americsa.</string>
    <string name="question_asia">Lake Baikal is the world \'s oldest and deepest freshwater lake.</string>


</resources>

这里写图片描述

如果R红了。检测下是否代码有错。方法很多,最简单的就是重启。
今天做实验用的eclipse。R出错的解决办法是先检测资源文件是否有错。很多同学是不理解,导致字符串文件都没写。。。。。这个解决后。删除import R;
然后 clearn 项目。一般就行了。还有一种!!!文件的位置放错了。。。帮人看了一阵才看出来。。。。

图片的话,直接拖入即可。还有那3个文件夹代表不同的清晰度~。 Android SDK还有自带的9-PATH处理图片的工具。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值