1.首先创建项目GeoQuiz,过程就不细说了......
2.activity_quiz.xml
1 <?xml version="1.0" encoding="utf-8"?>
2
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:gravity="center"
6 android:orientation="vertical"
7 >
8
10 android:layout_height="wrap_content"
11 android:padding="24dp"
12 android:text="@string/question_text"
13 />
14
16 android:layout_height="wrap_content">
17 android:orientation="horizontal"
18
20 android:layout_height="wrap_content"
21 android:text="@string/true_button"
22 />
23
25 android:layout_height="wrap_content"
26 android:text="@string/false_button"
27 />
28
29
3.添加字符串资源,字符串资源是放置 res/values目录下的strings.xml
1
2 GeoQuiz
3
4 Constantinople is the largest city in Turkey.
5 TRUE
6 FALSE
7
预览:
4.从布局XML到视图对象
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
10 @Override11 protected voidonCreate(Bundle savedInstanceState) {12 super.onCreate(savedInstanceState);13 setContentView(R.layout.activity_quiz);14 }15 }
5.为按钮添加资源id
6.添加成员变量
7.引用组件
8.设置监听器
9.增加toast字符串
10.创建提示消息
11.模拟器运行
最终代码:
Activity_quiz.xml
1 <?xml version="1.0" encoding="utf-8"?>
2
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:gravity="center"
6 android:orientation="vertical"
7 >
8
10 android:layout_height="wrap_content"
11 android:padding="24dp"
12 android:text="@string/question_text"
13 />
14
16 android:layout_height="wrap_content">
17 android:orientation="horizontal"
18
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:text="@string/true_button"
23 />
24
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:text="@string/false_button"
29 />
30
31
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;10
11 public classQuizActivity extends AppCompatActivity {12
13 privateButton mTrueButton;14 privateButton mFalseButton;15
16 @Override17 protected voidonCreate(Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19 setContentView(R.layout.activity_quiz);20
21 mTrueButton =(Button) findViewById(R.id.true_button);22 mTrueButton.setOnClickListener( newView.OnClickListener(){23
24 public voidonClick(View v){25 Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();26 }27
28 });29
30 mFalseButton =(Button) findViewById(R.id.false_button);31 mFalseButton.setOnClickListener( newView.OnClickListener(){32 public voidonClick(View v){33 Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();34 }35
36 });37
38 }39 }
strings.xml
1
2 GeoQuiz
3
4 Constantinople is the largest city in Turkey.
5 TRUE
6 FALSE
7 Correct!
8 Incorrect!
9
运行结果: