文章目录
前言
本章,我们为GeoQuiz应用添加第二个activity。一个activity控制一屏信息,新activity将带来 第二个用户界面,方便用户偷看当前问题的答案。
一.添加字符串资源(strings.xml)
<resources>
...
<string name=" Incorrect_toast"> Incorrect!</string>
<string name="warning_text">Are you sure you 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>
二.创建新的 activity
在随后弹出的对话框中,Activity Name处输入CheatActivity,如图5-4所示。这是Activity 子类的名字。Layout Name自动赋值为activity_cheat。这是向导为布局文件创建的基本名称.
三。第二个activity的布局组件定义(activity_cheat.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" tools:context="com.bignerdranch.android.geoquiz.CheatActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/warning_text"/>
<TextView android:id="@+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
tools:text="Answer"/>
<Button
android:id="@+id/show_answer_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_answer_button"/>
</LinearLayout>
四.在manifest配置文件中声明 activity
(AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.geoquiz" >
<application android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".QuizActivity">
<intent-filter>