您的位置:首页 > 移动开发 > Android开发

实验4-2:掌握Android应用调试方法、添加新界面

2014-09-28 22:04 204 查看

第五章、添加新界面

代码清单5-1 添加字符串资源(strings.xml)
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
...
<stringname="question_mount">中国最高的山---长白山.</string>
<stringname="cheat_button">作弊!</string>
<stringname="warning_text_view">你真的要这么做?</string>
<stringname="show_answer_button">显示答案</string>
<stringname="judgment_toast">作弊是不对滴.</string>
</resources>


代码清单5-2 第二个activity的布局组件定义(activity_cheat.xml)

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/warning_text_view"/>
<TextView
android:id="@+id/answerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"/>
<Button
android:id="@+id/showAnswerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_answer_button"/>
</LinearLayout>



代码清单5-3 覆盖onCreate(...)方法(CheatActivity.java)

publicclassCheatActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
}
}



代码清单5-4 在manifest配置文件中声明CheatActivity(AndroidManifest.xml)

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.GeoQuiz"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.bignerdranch.android.GeoQuiz.QuizActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".CheatActivity"
android:label="@string/app_name"/>
</application>



代码清单5-5 默认布局中添加cheat按钮(layout/activity_quiz.xml)

...
</LinearLayout>
<Button
android:id="@+id/cheat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheat_button"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"/>
</LinearLayout>



代码清单5-6 水平布局中添加cheat按钮(layout-land/activity_quiz.xml)

...
</LinearLayout>
<Button
android:id="@+id/cheat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="@string/cheat_button"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="@string/next_button"
android:drawableRight="@drawable/arrow_right"
android:drawablePadding="4dp"/>
</FrameLayout>



代码清单5-7 启用Cheat按钮(QuizActivity.java)




代码清单5-8 启动CheatActivity(QuizActivity.java)




代码清单5-9 添加extra常量(CheatActivity.java)




代码清单5-10 将extra附加到intent上(QuizActivity.java)




代码清单5-11 获取extra信息(CheatActivity.java)




代码清单5-12 启用作弊模式(CheatActivity.java)



代码清单5-13 调用startActivityForResult(...)方法(QuizActivity.java)




代码清单5-14 设置结果值(CheatActivity.java)




代码清单5-15 onActivityResult(...)方法的实现(QuizActivity.java)




代码清单5-16 基于mIsCheater变量值改变toast消息(QuizActivity.java)




代码清单5-17 QuizActivity被指定为launcheractivity(AndroidManifest.xml)



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: