android开发的第一个app--GeoQuiz

Android开发是什么

  Android开发是指android平台上应用的制作,Android早期由“Android之父”之称的Andy Rubin创办,Google于2005年并购了成立仅22个月的高科技企业Android,展开了短信、手机检索、定位等业务,以Java为编程语言,使接口到功能,都有层出不穷的变化。

此时此刻的感受

  第一次写博客,感觉很奇妙,想了好久,还没有思路,到底写什么呢?仍记得,初高中写作文时,假如对文题没有头绪,就会选择编个故事,把自己当作其中的主角,想其所想,悟其所悟,体会其中的冷暖寒凉,然后一篇1000字的文章就写完了。这个时候,想着想着,渐渐有所感悟。博客记录的是一个程序员进行项目开发的所思所悟,这个贵在积累,每天一点点,既是对自己的总结又能将自己的经历与网友共勉。所以现在想来,博客也并没有那么难了,因为这是为自己书写的。

GeoQuiz的项目开发历程

创建Android项目

注意:

1、选择合适的SDK版本

2、注意子类名的Activity后缀,这是种规范的命名方式

用户界面设计

1、在XML文件(activity_quiz.xml)中定义组件,为按钮添加资源ID

<?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:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"/>
        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/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/prev_button"
        android:text="@string/prev_button"/>
        <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_button"/>

    </LinearLayout>
    <Button
        android:id="@+id/cheat_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:text="@string/cheat_button"/>

</LinearLayout>

2、视图层级结构

LinearLayout有两个子组件:TextView、LinearLayout。

若作为子组件的LinearLayout,还自带两个button组件。

组件的俩属性:match_parent、wrap_content

从布局XML到视图对象

1、引用组件,为TRUE、FALSE按钮设置监听器、创建提示消息

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

        if(savedInstanceState !=null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX,0);
        }

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

        mTrueButton = (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
                checkAnswer(true);
            }
        });
        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
                checkAnswer(false);
            }
        });

2、接着可以尝试运行应用,界面图用最终的啦

注意:安卓手机首次连接时,手机需要在开发者选项,找到USB调试按钮并开启,手机连接也换成传送文件,并且注意点击手机上的提示信息,正确安装app

以小见大,融会贯通,实现app功能的完善与创新

1、Android与MVC设计模式

2、activity的生命周期

3、Android应用的调试

4、第二个activity

下面分别是各项的完整代码,仅供参考

CheatActivity

package edu.niit.software.geoquiz;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends AppCompatActivity {

    private static final String EXTRA_ANSWER_IS_TRUE="edu.niit.software.geoquiz.answer_is_true";
    private static final String EXTRA_ANSWER_SHOWN = "edu.niit.software.geoquiz.answer_shown";
    private boolean mAnswerIsTrue;

    private TextView mAnswerTextView;
    private Button mShowAnswerButton;

    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_SHOWN,false);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);

        mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);

        mAnswerTextView = (TextView) findViewById(R.id.answer_text_view);

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

  

Question

package edu.niit.software.geoquiz;

/**
 * Created by 666 on 2017/9/4.
 */

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;
    }
}

 QuizActivity

package edu.niit.software.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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 Button mPrevButton;
    private Button mCheatButton;
    private TextView mQuestionTextView;
    private static final String TAG = "QuizActivity";
    private static final String KEY_INDEX = "index";

    private static final int REQUEST_CODE_CHEAT = 0;

    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,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 boolean mIsCheater;

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

        if(savedInstanceState !=null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX,0);
        }

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

        mTrueButton = (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                //Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
                checkAnswer(true);
            }
        });
        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
               // Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
                checkAnswer(false);
            }
        });
        mNextButton = (Button) findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length;
                mIsCheater = false;
                updateQuestion();
            }
        });

        mPrevButton = (Button) findViewById(R.id.prev_button);
        mPrevButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                mCurrentIndex = (mCurrentIndex-1) % mQuestionBank.length;
                updateQuestion();
            }
        });

        mCheatButton = (Button) findViewById(R.id.cheat_button);
        mCheatButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                //Intent intent = new Intent(QuizActivity.this , CheatActivity.class);
                boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
                Intent intent = CheatActivity.newIntent(QuizActivity.this,answerIsTrue);
                //startActivity(intent);
                startActivityForResult(intent,REQUEST_CODE_CHEAT);
            }
        });
        updateQuestion();

    }

    @Override
    public void onStart(){
        super.onStart();
        Log.d(TAG,"onStart() called");
    }
    @Override
    public void onResume(){
        super.onResume();
        Log.d(TAG,"onResume() called");
    }
    @Override
    public void onPause(){
        super.onPause();
        Log.d(TAG,"onPause() called");
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG,"onSaveInstanceState");
        savedInstanceState.putInt(KEY_INDEX,mCurrentIndex);
    }
    @Override
    public void onStop(){
        super.onStop();
        Log.d(TAG,"onStop() called");
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }

    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(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();
    }

    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);
        }
    }
}

strings

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="question_australia">堪培拉是澳大利亚的首都 </string>
    <string name="question_oceans">太平洋比大西洋大</string>
    <string name="question_mideast">苏伊士运河连接着红海和印度洋</string>
    <string name="question_africa">尼罗河流域在埃及</string>
    <string name="question_americas">亚马逊河是美国最长的河</string>
    <string name="question_asia">贝加尔湖是世界上最古老最深的淡水湖</string>
    <string name="true_button">TRUE</string>
    <string name="false_button">FALSE</string>
    <string name="next_button">Next</string>
    <string name="prev_button">Prev</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
    <string name="warning_text">Are you sure you want to do this?</string>
    <string name="show_answer_button">Show Answer</string>
    <string name="cheat_button">Cheat!</string>
    <string name="judgment_toast">Cheating is wrong</string>
</resources>

  

  大体上就这么多,其中还遇到了一个麻烦,就是完成了快好的时候,被我一个不小心,全部删掉了,撤回不成,所以我又重写了一篇,如果其中有些许问题,望君海涵。

时间: 2024-08-21 13:26:27

android开发的第一个app--GeoQuiz的相关文章

android开发之第一个app程序

继续刚刚的讲,完成开发环境的搭配之后,我们就可以开始自己开发自己的应用程序了. 1.先熟悉一下整个开发环境的目录结构.PS:至于eclipse的使用在这里就不多说了,如果不会的,请自己去百度找相关的知识. 先新建一个项目: 然后: 就那个Required SDK一般选2.2之外,其他的所有都可以默认. 然后就可以看到目录结构了. 具体的我就不废话了,因为很多的基础知识在视频中都会知道,我就写下一些简单的目录介绍. src 这个目录就是用了存放java代码的地方,跟java的开发是一模一样的: g

(转)解决android开发人员,手机app图标显示不正确问题

android程序更换图标安装后不变解决办法 最近在搞android的时候发现,一开始程序使用系统默认图标,等到应用发布的时候要更换图标,结果在测试机上图标就是不变,其他手机和模拟器上都更新了图标. 测试机是小米,据说这个问题只在小米上会出现.网上查了原因说是miui会缓存图标,并且提供了两个解决方法 1.进入目录  /data/system/customized_icons 下,找到你原来的旧图标,删除即可.需要root权限 2.把当前的工程换一个包名,重新安装即可.换了包名等于是一个新的应用

Android开发图形处理创建一个图形的拷贝

定义一个布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:or

Android开发1、2周——GeoQuiz项目

GeoQuiz项目总结 通过学习Android基本概念与构成应用的基本组件,来开发一个叫GeoQuiz的应用.该应用的用途是测试用户的地理知识.用户单击TRUE或FALSE按钮来回答屏幕上的问题,GeoQuiz可即时反馈答案正确与否. 开发前的准备工作 想要开发一个Android应用,首先要在电脑上装上开发软件.在这里推荐Android Studio,本文所有的开发都是在该平台上进行的. Android Studio的安装包括: 1.Android SDK 最新版本的Android SDK. 2

Android开发实践:检测App的内存占用和泄漏

来源:http://www.linuxidc.com/Linux/2014-03/97563.htm 前段时间开发的Android应用,每次都是在运行了半个小时左右后突然挂掉了,很是莫名其妙,也不知道哪里出了问题,后来一步步排查,发现问题出在JNI层,一个被频繁调用的函数分配的内存忘记释放,导致内存泄漏. 这次问题使我明白,别以为Android程序是基于Java语言,有强大的垃圾回收机制,就完全不用担心内存问题,其实Android程序也要特别小心你的内存,因为毕竟手机不比PC机,内存是极其有限的

Android开发:自定义银行app的最大额度控件

详解 前几天看到掌上生活上一个好玩的最大额度提示的页面,作为程序员的我,不能光看别人做的效果,于是自己也撸了一个差不多的额度控件了. 掌上生活效果: 今天就是这么个玩意是个主角了.好了,下面也来看看咋们要实现的结果: 使用: xml: <com.xiangcheng.amount.AmountView android:id="@+id/amount_view" android:layout_width="wrap_content" android:layout

android开发实战-记账本APP(一)

记账本开发流程: 对于一个记账本的初步开发而言,我实现的功能有: ①实现一个记账本的页面 ②可以添加数据并更新到页面中 ③可以将数据信息以图表的形式展现 (一)首先,制作一个记账本的页面. ①在系统自动创建的content_main.xml文件中添加listview <ListView android:id="@+id/lv_main" android:layout_width="wrap_content" android:layout_height=&quo

android开发:把一个byte数组转换成wav音频文件,并且播放

============问题描述============ 如题,byte数组转换成wav音频文件,并且播放,下面代码能生成data/data/com.example.playwav/cache/temp.wav 但是在播放的时候报异常. 我把代码和Log贴在下面了. 我分析,原因应该是wav文件格式的编解码问题,不能这么随随便便把任意的一个byte数组就转化为了wav 希望了解wav编解码开发的童鞋给点解决办法 byte[] a = { 52, 51, 48, 28, 58, 64, 98,-1

Android开发学习笔记--一个有界面A+B的计算器

做了一个A+B的APP,虽然很简单,但是作为初学者还是弄了几个小时才弄好,什么东西都要看书或者百度,但最后成功了,还是很开心的,收货蛮大的.现在把过程写一下: 首先给出效果图: 一开始布局一直有问题,不知道为什么我定义了两个编辑框跟一个按钮,但画出来的时候全都重叠在左上角了,只能输入到一个编辑框,一直卡在这里,后来找了一个输入用户名密码的布局文件参考了一下,发现把原来生成的前面那些删掉,然后设置为垂直布局就不会重叠在一起了,正常画出来之后,代码部分就简单了,一共有三个变量,我把第三个显示结果的框