首页 > 代码库 > Android开发初体验--第一个activity-->GeoQuiz

Android开发初体验--第一个activity-->GeoQuiz

1.首先创建项目GeoQuiz,过程就不细说了......

技术分享

 

2.activity_quiz.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:gravity="center"
 6     android:orientation="vertical"
 7     >
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:padding="24dp"
12         android:text="@string/question_text"
13         />
14     <LinearLayout
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content">
17         android:orientation="horizontal"
18         <Button
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content"
21             android:text="@string/true_button"
22             />
23         <Button
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:text="@string/false_button"
27             />
28         </LinearLayout>
29 </LinearLayout>

3.添加字符串资源,字符串资源是放置 res/values目录下的strings.xml

技术分享

1 <resources>
2     <string name="app_name">GeoQuiz</string>
3 
4     <string name="question_text">Constantinople is the largest city in Turkey.</string>
5     <string name="true_button">TRUE</string>
6     <string name="false_button">FALSE</string>
7 </resources>

预览:

技术分享

 

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 class QuizActivity extends AppCompatActivity {
 9 
10     @Override
11     protected void onCreate(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 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:gravity="center"
 6     android:orientation="vertical"
 7     >
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:padding="24dp"
12         android:text="@string/question_text"
13         />
14     <LinearLayout
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content">
17         android:orientation="horizontal"
18         <Button
19             android:id="@+id/true_button"
20             android:layout_width="wrap_content"
21             android:layout_height="wrap_content"
22             android:text="@string/true_button"
23             />
24         <Button
25             android:id="@+id/false_button"
26             android:layout_width="wrap_content"
27             android:layout_height="wrap_content"
28             android:text="@string/false_button"
29             />
30         </LinearLayout>
31 </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;
10 
11 public class QuizActivity extends AppCompatActivity {
12 
13     private Button mTrueButton;
14     private Button mFalseButton;
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_quiz);
20 
21         mTrueButton = (Button) findViewById(R.id.true_button);
22         mTrueButton.setOnClickListener( new View.OnClickListener(){
23 
24             public void onClick(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( new View.OnClickListener(){
32             public void onClick(View v){
33                 Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
34             }
35 
36         });
37 
38     }
39 }

 

 

strings.xml

 

1 <resources>
2     <string name="app_name">GeoQuiz</string>
3 
4     <string name="question_text">Constantinople is the largest city in Turkey.</string>
5     <string name="true_button">TRUE</string>
6     <string name="false_button">FALSE</string>
7     <string name="correct_toast">Correct!</string>
8     <string name="incorrect_toast">Incorrect!</string>
9 </resources>

 

运行结果:

技术分享

 

Android开发初体验--第一个activity-->GeoQuiz