Android Development Experience-first Activity-->geoquiz

Source: Internet
Author: User

1. First create the project Geoquiz, the process will not elaborate ...

2.activity_quiz.xml

1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:gravity="Center"6android:orientation="Vertical"7>8<TextView9Android:layout_width="wrap_content"Tenandroid:layout_height="wrap_content" Oneandroid:padding="24DP" Aandroid:text="@string/question_text" -/> -<LinearLayout theAndroid:layout_width="wrap_content" -android:layout_height="wrap_content"> -android:orientation="Horizontal" -<Button +Android:layout_width="wrap_content" -android:layout_height="wrap_content" +android:text="@string/true_button" A/> at<Button -Android:layout_width="wrap_content" -android:layout_height="wrap_content" -android:text="@string/false_button" -/> -</LinearLayout> in</LinearLayout>

3. Add a string resource, the string resource is placed under the Res/values directory Strings.xml

1<resources>2<stringName="app_name">geoquiz</string>3 4<stringName="Question_text">constantinople isThe largest cityinchturkey.</string>5<stringName="True_button">true</string>6<stringName="False_button">false</string>7</resources>

Preview:

4. From the layout XML to the View object

Quizactivity.java

1 Package Com.bignerdranch.android.geoquiz;2 3 import android.support.v7.app.AppCompatActivity;4 import Android.os.Bundle;5 import Android.view.Menu;6 import Android.view.MenuItem;7 8  Public classQuizactivity extends Appcompatactivity {9 Ten @Override One     protected voidonCreate (Bundle savedinstancestate) { A super.oncreate (savedinstancestate); - Setcontentview (R.layout.activity_quiz); -     } the}

5. Add a resource ID to the button

6. Adding member variables

7. Referencing components

8. Setting up Listeners

9. Add Toast String

10. Create a prompt message

11. Simulator operation

Final code:

Activity_quiz.xml

1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:gravity="Center"6android:orientation="Vertical"7>8<TextView9Android:layout_width="wrap_content"Tenandroid:layout_height="wrap_content" Oneandroid:padding="24DP" Aandroid:text="@string/question_text" -/> -<LinearLayout theAndroid: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" Aandroid:text="@string/true_button" at/> -<Button -Android:id="@+id/false_button" -Android:layout_width="wrap_content" -android:layout_height="wrap_content" -android:text="@string/false_button" in/> -</LinearLayout> to</LinearLayout>

Quizactivity.java

1 Package Com.bignerdranch.android.geoquiz;2 3 import android.support.v7.app.AppCompatActivity;4 import Android.os.Bundle;5 import Android.view.Menu;6 import Android.view.MenuItem;7 import Android.view.View;8 import Android.widget.Button;9 import Android.widget.Toast;Ten  One  Public classQuizactivity extends Appcompatactivity { A  -     PrivateButton Mtruebutton; -     PrivateButton Mfalsebutton; the  - @Override -     protected voidonCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); + Setcontentview (R.layout.activity_quiz); -  +Mtruebutton =(Button) Findviewbyid (R.id.true_button); AMtruebutton.setonclicklistener (NewView.onclicklistener () { at  -              Public voidOnClick (View v) { -Toast.maketext (quizactivity. This-3.string. Incorrect_toast, Toast.length_short). Show (); -             } -  -         }); in  -Mfalsebutton =(Button) Findviewbyid (R.id.false_button); toMfalsebutton.setonclicklistener (NewView.onclicklistener () { +              Public voidOnClick (View v) { -Toast.maketext (quizactivity. ThisR.string. Correct_toast, Toast.length_short). Show (); the             } *  $         });Panax Notoginseng  -     } the}

Strings.xml

1<resources>2<stringName="app_name">geoquiz</string>3 4<stringName="Question_text">constantinople isThe largest cityinchturkey.</string>5<stringName="True_button">true</string>6<stringName="False_button">false</string>7<stringName="Correct_toast">correct!</string>8<stringName="Incorrect_toast">incorrect!</string>9</resources>

Operation Result:

Android Development Experience-first Activity-->geoquiz

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.