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

第五章、添加新界面

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


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

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns: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)

public class CheatActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_cheat);

}

}


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

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns: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>

<action android:name="android.intent.action.MAIN" />

<category android: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)

image

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

image

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

image

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

image

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

image

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

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

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

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

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

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

image

posted @ 2014-09-28 22:04  jlxuqiang  阅读(651)  评论(0编辑  收藏  举报