"The authoritative guide for Android Programming"-Reading Notes (iv)-geoquiz function extension

Source: Internet
Author: User

"The authoritative Guide to Android Programming"-Reading notes

-geoquiz Feature Extensions

From now on, the book began to expand its application. In this extension, we will learn the following points of knowledge:

    • Create a new class
    • Update the View layer
    • Update the control layer
    • Modification and submission of GIT code
    • Android Studio runs the app on your device
    • Add a picture resource to a button

Function: Is the Geoquiz Application object plot. The applied objects are divided into three parts by model, controller, and view categories. Android apps are designed to give model-controller-view (Model-view-controller, MVC) architectural patterns.

Create a class TrueFalse

This class defines 2 variables mquestion, mtruequestion.

Mquestion the resource ID used to hold the geography knowledge issue for payment wear.

Mtruequestion is used to determine whether the answer is correct.

Right-->new The class name is TrueFalse.

private int mquestion;

Private Boolean mtruequestion;

Add 2 private variables

Use the Fast key alt+insert to activate the select constructor and Getter and Setter.

git operation:

$git Add.

$git commit–m "Add a new class"

$git Push Geoquiz Master

You can view the code after submission

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

Update the View layer

Modify Activity_quiz.xml

Add an ID of @+id/question_text_view to TextView and add a button ID of @+id/next_button.

Update string resource definition (strings.xml)

Add a String

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

Update the control layer

First to affirm

Private Button Mnextbutton;

Private TextView Mquestiontextview;

The new button variable and the problem display space TextView, and then create an array of TrueFalse objects, because is an example program, so directly to him to create data.

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),

};

This data is created in the quizactivity, which is the control layer, which is not an optimal behavior in the real program.

Initializes the first question, in OnCreate ()

Mquestiontextview = (TextView) Findviewbyid (R.id.question_text_view);

int question = Mquestionbank[mcurrentindex].getquestion ();

Mquestiontextview.settext (question);

This code first obtains the TextView reference, and then through SetText (resource ID), displays the problem. The resource ID is of type int.

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

}

});

This code first gets the Nextbutton reference, then binds it to the click event, changes the problem once per click, and then gets the resource ID of the new problem, which refreshes the problem by SetText (resource ID).

Here the function has been able to do every click Next will change a problem, the effect is as follows:

After clicking Next:

We submit the code once, this time the code modifies the resource file, modifies the interface and modifies the interaction code.

GIT operates as follows:

Right-click Git Bush under the working directory

$git status

$git Add.

$git status

First commit the changes

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

Then submit to Git.oschina.net

$git Push Geoquiz Master

The code is modified as follows

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

Refactoring of code

The code that updates the Mquestiontextview variable is distributed in different places. The book guides us through the first simple refactoring, which puts the bow code of the processing in a separate private method.

private void Updatequestion () {

int question = Mquestionbank[mcurrentindex].getquestion ();

Mquestiontextview.settext (question);

}

Add a private method updatequestion and then replace the duplicated place.

git operations

$git status

$git Add.

$git commit–m "Refactoring Code"

$git Push Geoquiz Master

After refactoring the code is as follows:

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

Verifying the correctness of the problem

Create a Private method 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 ();

}

Replace the event code inside the True_button,false_button with this method. The code is as follows:

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

After implementing this feature, if an incorrect answer is selected, you will be prompted to Incorect. As shown in the following:

Android Studio runs the app on your device

Click Run->edit configurations ...

Check the USB device in target device (the default is Show Chooser Diaglog).

Click OK. Then Run->run ' app '

Add a picture resource to a button

Look for a picture arrow_right.png modify Next button's code as follows:

<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"/>

The display results are as follows:

Committing code in Git

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

"The authoritative guide for Android Programming"-Reading Notes (iv)-geoquiz function extension

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.