android開發的第一個app--GeoQuiz

來源:互聯網
上載者:User

標籤:user   重寫   科技   warning   子類   定義   boolean   ubi   pac   

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>

  

  大體上就這麼多,其中還遇到了一個麻煩,就是完成了快好的時候,被我一個不小心,全部刪掉了,撤回不成,所以我又重寫了一篇,如果其中有些許問題,望君海涵。

 

android開發的第一個app--GeoQuiz

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.