《Android編程權威指南》-讀書筆記(四)-GeoQuiz功能擴充

來源:互聯網
上載者:User

標籤:

《Android編程權威指南》-讀書筆記

-GeoQuiz功能擴充

 

從現在開始,這本書開始擴充應用。在這次擴充中我們將會學習以下知識點:

  • 建立一個新類
  • 更新視圖層
  • 更新控制層
  • Git代碼的修改和提交
  • Android Studio 在裝置中運行該應用
  • 給按鈕添加圖片資源

 

功能:是GeoQuiz應用對象圖解。應用的對象按模型、控制器和視圖的類別被分為三部分。Android應用是給予模型-控制器-視圖(Model-View-Controller,簡稱MVC)的架構模式進行設計的。

 

 

建立一個類 TrueFalse

這個類定義了2個變數 mQuestion、mTrueQuestion。

mQuestion用來儲存地理知識問題支付穿的資源ID。

mTrueQuestion用來確定答案正確與否。

右鍵->new class 類的名稱為TrueFalse。

private int mQuestion;

private boolean mTrueQuestion;

添加2個private 的變數

使用快鍵 alt+Insert 啟用選擇Constructor 和 Getter and Setter。

Git操作:

$git add .

$git commit –m "add a new class"

$git push GeoQuiz master

 

提交之後可以查看代碼

http://git.oschina.net/canglin/GeoQuiz/commit/eea5ae599c3830daaf49eb6ac380b49a2fc07193

 

 

更新視圖層

修改activity_quiz.xml

給TextView添加一個id為@+id/question_text_view,添加一個按鈕id為@+id/next_button。

 

更新字串資源定義(strings.xml)

添加字串

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

 

更新控制層

首先申明

private Button mNextButton;

private TextView mQuestionTextView;

新增的按鈕變數和問題顯示空間TextView,然後建立一個TrueFalse的對象數組,因為是例子程式,所以直接給他建立資料。

private TrueFalse[] mQuestionBank = new TrueFalse[]

{

new TrueFalse(R.string.question_oceans,true),

new TrueFalse(R.string.question_mideast,false),

new TrueFalse(R.string.question_africa,false),

new TrueFalse(R.string.question_americas,true),

new TrueFalse(R.string.question_asia,true),

};

這個資料建立在了QuizActivity裡面,這裡是控制層,在真實的程式裡這是不優的行為。

 

初始化第一個問題,在onCreate()中

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

int question = mQuestionBank[mCurrentIndex].getQuestion();

mQuestionTextView.setText(question);

這段代碼首先得到TextView的引用,然後通過setText(資源id),顯示問題。資源id為int類型。

添加NextButton

mNextButton = (Button)findViewById(R.id.next_button);

mNextButton.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View v){

mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length;

int question = mQuestionBank[mCurrentIndex].getQuestion();

mQuestionTextView.setText(question);

}

});

這段代碼首先得到NextButton的引用,然後給它綁定click事件,每點擊一次更換一次問題,然後得到這個新問題的資源id,在通過setText(資源id)來重新整理問題。

 

到這裡功能已經能做到每次點擊Next就會換一個問題,效果如下:

點擊NEXT後:

我們提交一次代碼,這次代碼中修改了資源檔,修改了互動介面,修改了互動代碼。

Git操作如下:

在工作目錄下右鍵 Git Bush

$git status

$git add .

$git status

首先提交修改

$git commit –m "Add a new button,adds several new issues,adds a new button event"

然後提交到git.oschina.net

$git push GeoQuiz master

代碼的修改情況如下

http://git.oschina.net/canglin/GeoQuiz/commit/6be7ee83e2a92f11fd3e4c73072c0e2ef191b162

 

 

代碼的重構

在更新mQuestionTextView變數的代碼分布在了不同的地方。書中指引我們做了第一次簡單的重構,將處理的弓弓代碼放在了單獨的私人方法裡面。

private void updateQuestion(){

int question = mQuestionBank[mCurrentIndex].getQuestion();

mQuestionTextView.setText(question);

}

添加私人方法updateQuestion然後替換掉重複的地方。

Git操作

$git status

$git add .

$git commit –m "refactoring code"

$git push GeoQuiz master

重構後代碼如下:

http://git.oschina.net/canglin/GeoQuiz/commit/0fcfdcf2fa5e699ebf2e145ed9d468c3f9d28599

 

驗證問題的正確性

建立一個私人的方法checkAnswer()

private void checkAnswer(boolean userPresedTrue) {

boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();

int messageResId = 0;

if(userPresedTrue == answerIsTrue) {

messageResId = R.string.correct_toast;

} else {

messageResId = R.string.incorrect_toast;

}

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

}

用此方法替換掉true_button,false_button裡面的事件代碼。代碼如下:

http://git.oschina.net/canglin/GeoQuiz/commit/03989c691e966b1b5d79975993ed114722cb1365

實現此功能後,如果一個錯誤的答案你選擇了TRUE後,會提示incorect。如所示:

 

Android Studio在裝置中運行該應用

點擊 Run->Edit Configurations…

在Target Device 裡面勾選USB device(預設的是Show chooser diaglog)。

點擊確定。然後Run->Run ‘app‘

 

給按鈕添加圖片資源

隨便找一個圖片arrow_right.png 修改next button的代碼如下:

<Button

android:id="@+id/next_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/next_button"

android:drawableRight="@drawable/arrow_right"/>

顯示效果如下:

在git中提交代碼

http://git.oschina.net/canglin/GeoQuiz/commit/eea0734423d7c2bee60d74430b0a26904a3ce61c

《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.