GeoQuiz项目总结


  • 用户界面设计
  • Android 与MVC模式
  • Activity生命周期
  • 第二个Activity
  • 心得体会

用户界面设计

用户界面是由组件构造而成的,它可以显示文字或图像,与用户交互,甚至布置屏幕上的其他组件。默认的activity布局是由RelativeLayoutTextView两个组件构成。且一个activity只能有一个LinearLayout根元素,作为根元素,必须指定Android XML的资源文件的命名空间(namespace)属性。


Android 与MVC模式

Android 应用是基于MVC模式开发的,模型——视图——控制器的架构模式。

模型对象:存储应用的数据和业务逻辑
视图对象:用户界面,由各类组件构成
控制器对象:包含应用的逻辑单元,响应视图对象触发的事件,管理视图对象和模型对象之间的数据流动。(Activity、Fragment、Service)

更新视图层

增加一个Next按钮

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

更新字符串资源

<string name="next_button">Next</string>

新增问题字符串

    <string name="question_australia">Canberra is the capital of Ausrtalia .</string>
    <string name="question_oceans">The Pacific Ocean is larger than the Atlanic 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 longer river in the Americas.</string>
    <string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>

更新控制器层

增加按钮变量及Question对象数组

private TextView mQuestionTextView;

private Question[] mQuestionBank = new Question[] {
      new Question(R.string.question_australia,true),
            new Question(R.string.question_oceans,true),
            new Question(R.string.question_mideast,true),
            new Question(R.string.question_africa,true),
            new Question(R.string.question_americas,true),
            new Question(R.string.question_asia,true)
    };

使用UpdateQuestion()封装公共代码

mNextButton =(Button)findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                mCurrentIndex =(mCurrentIndex + 1)%mQuestionBank.length;
                mIsCheater = false;
                //int question = mQuestionBank[mCurrentIndex].getmTextResId();
                //mQuestionTextView.setText(question);
                updateQuestion();
            }
        });

        mCheatButton = (Button) findViewById(R.id.cheat_button);
        mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Start CheatActicity
                //Intent intent=new Intent(QuizActivity.this,CheatActivity.class);
                boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismAnswerTrue();
                Intent intent = CheatActivity.newIntent(QuizActivity.this,answerIsTrue);
                //startActivity(intent);
                startActivityForResult(intent,REQUEST_CODE_CHEAT);
            }
        });
        updateQuestion();
    }
    
    
     private void updateQuestion() {
        Log.d(TAG,"updating question",new Exception());
        int question = mQuestionBank[mCurrentIndex].getmTextResId();
        mQuestionTextView.setText(question);
    }

增加checkAnswer()方法——判断用户答案的正确性,修正之前代码的逻辑性错误(认为所有答案都是true)

 private void checkAnswer(boolean userPressedTrue) {
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismAnswerTrue();
        int messageResId = 0;
        if (mIsCheater) {
            messageResId = R.string.judgment_toast;
        }else {
            if (userPressedTrue == answerIsTrue) {
                messageResId = R.string.correct_toast;
            }else {
                messageResId = R.string.incorrect_toast;
            }
        }


        Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();
    }

调用checkAnswer()方法,在按钮的监听器里

mTrueButton = (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(QuizActivity.this,
                       // R.string.correct_toast,
                        //Toast.LENGTH_SHORT).show();
                //Dose nothing yet,but soon!
                checkAnswer(true);

            }
        });
        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // Toast.makeText(QuizActivity.this,
                 //       R.string.incorrect_toast,
                   //     Toast.LENGTH_SHORT).show();
                //Does nothing yet, but soon!
                checkAnswer(false);
            }
        });

Acticity的生命周期

activity的状态:不存在、停止、暂停、运行

用户可以与当前运行状态下的activity交互。但是任何时候只有一个activity能与用户进行交互。

activty的生命周期回调函数:onCreate()onDestory()onStart()onStop()onResume()onPause().

通过这几种方法可以在activity的生命周期状态发生关键性转换时完成某些工作。

日志跟踪理解activity生命周期

由于覆盖方法会输出日志,我们可以通过覆盖activity生命周期方法来了解activity的状态变化情况。

输出日志信息

新增一个日志常量

private static final String TAG = "QuizActivity";

onCreate(Bundle)方法添加日志输出代码

...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        **Log.d(TAG,"onCreate(Bundle) calles");**
        setContentView(R.layout.activity_quiz);
...

覆盖更多生命周期方法

...
@Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"onStart() called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"onResume() called");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"onPause() called");
    }
    
     @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"onStop() called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestory() called");
    }
    ...

创建水平模式布局

1.右键单击res目录后选择New->Android resources directory ,选择layout资源类型
2.将待选资源特征列表中的Orientation单击>>按钮将其移动到已选资源特征区域中。
3.选中Screen orentation中的Landsacpe选项,目录名为layout-land

  • Android 视图下的看不到res/layout-land目录,需要切换到Project视图下。

第二个Activity

一个activity控制一屏信息,新activity是用来方便用户偷看答案的

  • 启动activity:基于intent的通信——intent对象是compoment用来与操作系统通信的一种媒介工具
  • activity的数据传递:使用intent extra ,从子activity获取返回结果

具体代码如下:
QuizActivity.java

    private static final String KEY_INDEX = "index";
    private static final int REQUEST_CODE_CHEAT = 0;
    
    ...
    mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Start CheatActicity
                //Intent intent=new Intent(QuizActivity.this,CheatActivity.class);
                boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismAnswerTrue();
                Intent intent = CheatActivity.newIntent(QuizActivity.this,answerIsTrue);
                //startActivity(intent);
                startActivityForResult(intent,REQUEST_CODE_CHEAT);
            }
        });
        
        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CODE_CHEAT) {
            if (data == null) {
                return ;
            }
            mIsCheater = CheatActivity.wasAnswerShown(data);
        }
    }
    ...

CheatActivity.java

private static final String EXTRA_ANSWER_IS_TRUE ="cn.happy.qxy.geoquiz.answer_is_true";
private boolean mAnswerIsTrue;

 public static Intent newIntent(Context packageContext,boolean answerIsTrue) {
        Intent intent = new Intent(packageContext,CheatActivity.class);
        intent.putExtra(EXTRA_ANSWER_IS_TRUE,answerIsTrue);
        return intent;
    }

 public static boolean wasAnswerShown(Intent result) {
        return result.getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
    }

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

        mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
        mAnswerTexView = (TextView) findViewById(R.id.answer_text_view);

        mShowAnswerButton = (Button) findViewById(R.id.show_answer_button);
        mShowAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAnswerIsTrue) {
                    mAnswerTexView.setText(R.string.true_button);
                }else {
                    mAnswerTexView.setText(R.string.false_button);
                }
                setAnswerShownResult(true);
            }
        });
    }
    private void setAnswerShownResult(boolean isAnswerShown) {
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_IS_TRUE,isAnswerShown);
        setResult(RESULT_OK,data);
    }

心得体会

以上就是本次项目一些重要的步骤,及详细的代码展示。麻雀虽小,组脏俱全。
了解到Android Studio 的独有的优势:

  • 速度优于Eclipse
  • 提示补全更加人性化
  • 支持Google Cloud Platform
  • 强大的UI编辑器
  • 受欢迎的第三方GenyMoton虚拟机
  • Android Studio特有的异常调试工具Android Lint
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,104评论 25 707
  • 1.什么是Activity?问的不太多,说点有深度的 四大组件之一,一般的,一个用户交互界面对应一个activit...
    JoonyLee阅读 5,731评论 2 51
  • 这个时代,选择太多,变化太快,每一次站在分岔路口,都是一次大考验,纠结,郁闷,迷茫,都会无端地浪费大把光阴,而得不...
    丁少小蕾Melody阅读 929评论 0 7
  • 前两天听同学提起这部电影,是吴天明导演的电影,不过票房惨败,美团猫眼上的评分都很低,不过还是想去欣赏一下,就像去欣...
    乐君陶陶阅读 1,304评论 0 1
  • 有些感情像烂了的水果 水果水分多,营养丰富。 正如我们认识的六年里,我吃过剥好皮的橙子觉得甜,我吃过削下来的苹果皮...
    汤芍儿阅读 191评论 0 0