• 实验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

  • 相关阅读:
    针对wamp的phpmyadmin显示#2003无法连接mysql
    vs2019编译gdal3.1.0报错 (filemanager.obj) : error LNK2001: 无法解析的外部符号 __imp_SHGetFolderPathW
    半透明遮罩层覆盖整个可视区域
    css首字下沉
    仿花瓣标题栏始终在页面顶部(ie6下position:fixed失效解决方法)
    Redis最佳实践及核心原理
    Java对接微信公众号模板消息推送
    XXLJOB任务调度
    MyBatis学习笔记
    SpringBoot集成Redis
  • 原文地址:https://www.cnblogs.com/jlxuqiang/p/3999098.html
Copyright © 2020-2023  润新知