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