Android developed the first App--geoquiz

Source: Internet
Author: User

What is Android development?

Android development refers to the production of Android platform applications, Android early by the "Father of Android," said Andy Rubin founded, Google in 2005 to acquire a 22-month-old high-tech enterprise Android, launched a text message, mobile search, Positioning and other services, Java as a programming language, so that the interface to the function, there are endless changes.

The feeling at this moment

The first time to write a blog, feel very strange, think for a long time, there is no idea, in the end what to write it? Still remember, the first high school writing, if there is no clue to the text, will choose to make up a story, as one of the protagonist, think of its thinking, understand its understanding, and realize the warm and cold cold, and then a 1000-word article is finished. This time, want to think about, gradually have some sentiment. Blog record is a programmer for the project development of the thought of understanding, this expensive in the accumulation, a little bit each day, both to their own summary and can be their own experience and friends. So now it's not that hard to think about blogging because it's written for you.

Geoquiz project development process create an Android project

Attention:

1. Select the appropriate SDK version

2. Notice the activity suffix of the subclass name, which is the canonical naming method

User interface design 1, defining components in an XML file (activity_quiz.xml), adding a resource ID to a button
<?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. View Hierarchy

LinearLayout has two sub-components: TextView, LinearLayout.

If LinearLayout as a subassembly, it also comes with two button components.

Two properties of the component: Match_parent, wrap_content

From layout XML to view object 1, referencing component, setting listener for true, False button, creating prompt message
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.ma                Ketext (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.M                Aketext (Quizactivity.this,r.string.correct_toast,toast.length_short). Show (); CheckansweR (FALSE); }        });
2, then you can try to run the application, the interface diagram with the final

Note: When the Android phone is connected for the first time, the phone needs the developer option, find the USB debug button and turn it on, the phone connection is also transferred to the file, and pay attention to click on the phone information, the correct installation of the app

Sprat, Mastery, realize the perfection and innovation of app function 1, Android and MVC design mode 2, Activity life Cycle 3, Android application debugging 4, second activity

The following are the complete code for each item, for reference only 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 St    atic 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 (Packageco        Ntext,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 ques       tion (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.st                Ring.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) {mcurrent                Index = (mcurrentindex+1)% Mquestionbank.length;                Mischeater = false;            Updatequestion ();        }        });        Mprevbutton = (Button) Findviewbyid (R.id.prev_button); Mprevbutton.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {mcurrent                Index = (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 (Savedinstan        Cestate);        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].isanswe        Rtrue ();        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" > Canberra is the capital of Australia </string> <string name= "Question_oceans" > Pacific greater than the Atlantic Ocean </string> <string name= "Question_ Mideast "> Suez Canal connecting the Red Sea and Indian Ocean </string> <string name=" Question_africa "> Nile Valley in Egypt </string> <string Name= "Question_americas" > Amazon is the longest river </string> <string name= "Question_asia" in the United States > Lake Baikal is the oldest and deepest freshwater lake in the world </ 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> &L T;string name= "Correct_toast" >Correct!</string> <string name= "Incorrect_toast" >incorrect!</ string> <string name= "Warning_text" >are you sure your 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>

  

Basically so much, which also encountered a trouble, is finished quickly good time, was I a careless, all deleted, withdraw not, so I rewrite an article, if there are some problems, look June Haihan.

Android developed the first App--geoquiz

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.