Android項目開發——GeoQuiz項目總結

來源:互聯網
上載者:User

標籤:訊息   開發人員   source   bsp   sdk   label   coding   還需要   protected   

GeoQuiz項目總結

通過學習Android基本概念與構成應用的基本組件,來開發一個叫GeoQuiz的應用。該應用的用途是測試使用者的地理知識。使用者單擊TRUE或FALSE按鈕來回答螢幕上的問題,GeoQuiz可即時反饋答案正確與否。

開發前的準備工作

想要開發一個Android應用,首先要在電腦上裝上開發軟體。在這裡推薦Android Studio,本文所有的開發都是在該平台上進行的。

Android Studio的安裝包括:

1.Android SDK

最新版本的Android SDK。

2.Android SDK工具和平台工具

用來測試與調試應用的一套工具。

3.Android模擬器系統鏡像

用來在不同虛擬設備上開發測試應用。

 下載與安裝可以從Android開發人員網站下載Android Studio:https://developer.android.com/sdk/。首次安裝的話,你還需要從http://www.oracle.com下載並安裝Java開發人員套件(JDK7)。如仍有安裝問題,請訪問網址https://developer.android.com/sdk/尋求協助。下載早期版本的SDKAndroid Studio內建最新版本的SDK和系統模擬器鏡像。但若想在Android早期版本上測試應用,還需額外下載相關工具組件。可通過Android SDK管理器來配置安裝這些組件。在Android Studio中,選擇To o ls→Android→SDK Manager功能表項目,0-1所示。                              0-1  Andriod SDK管理器選擇並安裝需要的Android版本和工具。下載這些組件需要一定時間,請耐心等待。通過Android SDK管理器,還可以及時擷取Android最新發行內容,比如新系統平台或新版本工具等。應用開發1.1 項目的建立

首先,開啟Android Studio,建立GeoQuiz項目。

然後是這次項目的一個目錄。如:

1.2 代碼的編寫1.2.1 介面設計

本項目所需要的介面如下所示。

1.2.2 源碼

首先先在spring.xml中將設定處理好。

 1 <resources> 2     <string name="app_name">GeoQuiz</string> 3  4     <string name="true_button">TRUE</string> 5     <string name="false_button">FALSE</string> 6     <string name="next_button">NEXT</string> 7     <string name="prev_button">PREV</string> 8     <string name="correct_toast">Correct!</string> 9     <string name="incorrect_toast">Incorrect!</string>10     <string name="warning_text">Are you sure you want to do this?</string>11     <string name="show_answer_button">Show Answer</string>12     <string name="cheat_button">Cheat!</string>13     <string name="judgment_toast">Cheating is wrong.</string>14     <string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>15     <string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>16     <string name="question_africa">The source of the Nile River is in Egypt.</string>17     <string name="question_americas">The Amazon River is the longest river in the Americas.</string>18     <string name="question.asia">Lake Baikal is the world\‘s oldest and deepest freshwater lake.</string>19 </resources>

 

在QuizActivity.java中實現從布局到視圖。

 1     @Override 2     protected void onCreate(Bundle savedInstanceState) { 3         super.onCreate(savedInstanceState); 4         Log.d(TAG, "onCreate(Bundle) called"); 5         setContentView(R.layout.activity_quiz); 6  7         if (savedInstanceState != null) { 8             mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0); 9         }10 11         mQuestionTextView = (TextView) findViewById(R.id.question_text_view);12 13         mTrueButton = (Button) findViewById(R.id.true_button);14         mTrueButton.setOnClickListener(new View.OnClickListener() {15             @Override16             public void onClick(View v){17                 checkAnswer(true);18             }19         });20         mFalseButton = (Button) findViewById(R.id.false_button);21         mFalseButton.setOnClickListener(new View.OnClickListener() {22             @Override23             public void onClick(View v){24                 checkAnswer(false);25             }26         });27 28         mNextButton = (Button) findViewById(R.id.next_button);29         mNextButton.setOnClickListener(new View.OnClickListener() {30             @Override31             public void onClick(View v) {32                 mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;33                 mIsCheater = false;34                 updateQuestion();35             }36         });37 38         mPrevButton = (Button) findViewById(R.id.prev_button);39         mPrevButton.setOnClickListener(new View.OnClickListener() {40             @Override41             public void onClick(View v) {42                 mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;43                 updateQuestion();44             }45         });46 47         mCheatButton = (Button)findViewById(R.id.cheat_button);48         mCheatButton.setOnClickListener(new View.OnClickListener() {49             @Override50             public void onClick(View v) {51                 boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismAnswerTrue();52                 Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);53                 startActivityForResult(intent, REQUEST_CODE_CHEAT);54             }55         });56 57         updateQuestion();58 59     }

 在Question.java模型層代碼

 1 public class Question { 2     private int mTextResId; 3     private boolean mAnswerTrue; 4  5     public Question(int textResId, boolean answerTrue){ 6         mTextResId = textResId; 7         mAnswerTrue = answerTrue; 8     } 9 10     public int getmTextResId() {11         return mTextResId;12     }13 14     public void setmTextResId(int mTextResId) {15         this.mTextResId = mTextResId;16     }17 18     public boolean ismAnswerTrue() {19         return mAnswerTrue;20     }21 22     public void setmAnswerTrue(boolean mAnswerTrue) {23         this.mAnswerTrue = mAnswerTrue;24     }25 }
1.3 Android與MAC設計模式展示了在響應使用者單擊按鈕等事件時,對象間的互動控制資料流。注意,模型對象與視圖對象不直接互動。控制器作為它們之間的聯絡紐帶,接收對象發送的訊息,然後向其他對象發送操作指令。

建立數組對象,通過與Textview和button互動,在螢幕上顯示問題,並且對使用者的回答做出反應。

1.4 activity的生命週期

Activity的狀態圖解

1.5 第二個Activity

activity_cheat.xml中的組件代碼

 1 <?xml version="1.0" encoding="utf-8"?> 2  3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4                 xmlns:tools="http://schemas.android.com/tools" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     android:orientation="vertical" 8     android:gravity="center" 9     tools:context="classroom.geoquiz.CheatActivity">10 11     <TextView12         android:layout_width="wrap_content"13         android:layout_height="wrap_content"14         android:padding="24dp"15         android:text="@string/warning_text"/>16 17     <TextView18         android:layout_width="wrap_content"19         android:layout_height="wrap_content"20         android:id="@+id/answer_text_view"21         android:padding="24dp"22         tools:text="Answer"/>23 24     <Button25         android:id="@+id/show_answer_button"26         android:layout_width="wrap_content"27         android:layout_height="wrap_content"28         android:text="@string/show_answer_button"/>29 30 </LinearLayout>

 

 建立了第二個Activity,目的是為了方便使用者查看答案。

這是第二個activity的組件。

1.6 啟動activity

要啟動activity需要通過startActivity()方法。

Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);                startActivityForResult(intent, REQUEST_CODE_CHEAT);

 

 

Android項目開發——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.