安卓移动-作业4

实验报告

1 实验目的 (Experiment Purpose)

Get familiar with designing Android screens using layouts, widgets and/or the webview.

2 实验环境 (Experiment Environment)

software: java version “18.0.2”,Android Studio 2021.2.1 Patch 2
Operating system:Window 10

3 实验内容 (Experiment content)

3.1 Experimental data

Set the string in Android/app/res/layout/value/strings.xml.
在 Android/app/res/layout/value/strings.xml中制定字符串。

<resources>
    <string name="app_name">Intent</string>
    <string name="question_australia">Canberra is the capital of Australia.</string>
    <string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
    <string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
    <string name="question_africa">The source of the Nile River is in Egypt.</string>
    <string name="question_americas">The Amazon River is the longest river in the Americas.</string>
    <string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>
    <string name="question_none">There are no more questions.Continue clicking to return to the first question!</string>
    <string name="cheat_text">Do you really want to cheat?</string>
    <string name="cheat_button">Cheat</string>
    <string name="true_button">True</string>
    <string name="false_button">False</string>
    <string name="next_button">Next</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
</resources>

Set the images in Android/app/res/drawable.
在 Android/app/res/drawable中放置图片。

3.2 Experimental process

Set the layout in Android/app/res/layout/activity_main.xml.
在 Android/app/res/layout/activity_main.xml文件中设定布局。

As shown in the figure above, the overall layout of the Intent project
is vertical, including two vertical layouts A and B.
Vertical layout A contains a TextView, a horizontal layout (including
two buttons “TRUE” and “FALSE”), and a button (button “NEXT”).
Vertical layout B contains a button, namely “CHEAT”.
如上图,Intent 项目总体是垂直布局,其中包含两个纵向布局A和B。
纵向布局A中包含一个文本框、一个横向布局(其中包含两个按钮"TRUE"和"FALSE")、一个按钮。
纵向布局B中包含一个按钮,即"CHEAT"。
Set the function in
Android\app\src\main\java\com\example\imagebutton\MainActivity.java.
在 function in
Android\app\src\main\java\com\example\imagebutton\MainActivity.java文件中设置功能。

  • 定义全局变量
private Context mContext;
final int[] question_index = {0};
final String[][] question_answer={{"回答正确","回答错误"},{"回答错误","回答正确"},{"回答正确","回答错误"},{"回答正确","回答错误"},{"回答错误","回答正确"},{"本题无答案","本题无答案"}};
final String[] answer = {"True","False","True","True","False"};
  • 定义ArrayList<String\用于存放问题
ArrayList<String> questions = new ArrayList<>();
questions.add(getString(R.string.question_australia));
questions.add(getString(R.string.question_mideast));
questions.add(getString(R.string.question_africa));
questions.add(getString(R.string.question_americas));
questions.add(getString(R.string.question_asia));
questions.add(getString(R.string.question_none));
  • 定义图中出现的5个按钮
TextView question = findViewById(R.id.question_text_view);
Button true_button = findViewById(R.id.true_button);
Button false_button = findViewById(R.id.false_button);
Button next_button = findViewById(R.id.next_button);
Button cheat_button = findViewById(R.id.cheat_button);
  • 为"TRUE"按钮设置监听器
true_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(),question_answer[question_index[0]][0],Toast.LENGTH_SHORT).show();
    }
});
  • 为"FALSE"按钮设置监听器
false_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(),question_answer[question_index[0]][1],Toast.LENGTH_SHORT).show();
    }
});
  • 为"NEXT"按钮设置监听器
next_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        question_index[0] = (question_index[0] +1)%6;
        question.setText(questions.get(question_index[0]));
    }
});
  • 为"CHEAT"按钮设置监听器
cheat_button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          showAlertDialog();
      }
  });
}
  • 当用户选择作弊时,弹出对话框,询问用户是否确定作弊
private void showAlertDialog(){
    AlertDialog.Builder adBuilder = new AlertDialog.Builder(mContext);
    adBuilder.setTitle("对话框");
    adBuilder.setMessage("你确定要作弊吗");
    adBuilder.setPositiveButton("是", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            showAlertDialog_answer();
        }
    });
    adBuilder.setNegativeButton("否",null);
    adBuilder.show();
}
  • 当用户确定作弊,弹出对话框,显示答案。
private void showAlertDialog_answer(){
      AlertDialog.Builder adBuilder = new AlertDialog.Builder(mContext);
      adBuilder.setTitle("答案");
      adBuilder.setMessage(answer[question_index[0]]);
      adBuilder.setPositiveButton("确认",null);
      //adBuilder.setNegativeButton("取消",null);
      adBuilder.show();
  }
}
3.3 Experimental results

The results display and code implementation are shown as follows:
结果展示和代码实现如下图:

3.4 Analysis
  • In the button listener, you can directly define the Toast for prompt
    information The makeText () and dialog

  • AlertDialog dialogs have three buttons: OK, Negative, and Neutral.
    You can define any action after clicking the button, or you can not
    define it directly.

  • 按钮监听器中,可以直接定义对提示信息Toast.makeText()和对话框AlertDialog

  • 对话框的有三种按钮:确定、否定和中性按钮。可以任意定义按钮点击后动作,也可以直接不定义。

4 实验小结 (Summary)

My thoughts and experiences

  • You can set various colors such as “title bar” and “task bar” in
    “colorPrimary” and “colorPrimaryVariant” in atheme to make the
    page more beautiful.

  • 可以在theme中的"colorPrimary"和"colorPrimaryVariant"等设置各种"标题栏","任务栏"等颜色,使页面更加美观。

5 附录 (Appendix)

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:gravity="center"
        android:orientation="vertical">
        <TextView
            android:text ="@string/question_australia"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:id="@+id/question_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="24dp"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/true_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/true_button"
                android:background="@color/teal_700"
                android:layout_margin="10dp"/>

            <Button
                android:id="@+id/false_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/false_button"
                android:background="@color/teal_700"
                android:layout_margin="10dp"/>
        </LinearLayout>
        <Button
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next_button"
            android:drawableEnd="@drawable/arrow_right"
            android:drawablePadding="4dp"/>
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <Button
            android:id="@+id/cheat_button"
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:text="@string/cheat_button"
            android:background="#50616d"/>

    </LinearLayout>



</LinearLayout>
MainActivity.java
package com.example.intent;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private Context mContext;
    final int[] question_index = {0};
    final String[][] question_answer={{"回答正确","回答错误"},{"回答错误","回答正确"},{"回答正确","回答错误"},{"回答正确","回答错误"},{"回答错误","回答正确"},{"本题无答案","本题无答案"}};
    final String[] answer = {"True","False","True","True","False"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<String> questions = new ArrayList<>();
        questions.add(getString(R.string.question_australia));
        questions.add(getString(R.string.question_mideast));
        questions.add(getString(R.string.question_africa));
        questions.add(getString(R.string.question_americas));
        questions.add(getString(R.string.question_asia));
        questions.add(getString(R.string.question_none));

        TextView question = findViewById(R.id.question_text_view);
        Button true_button = findViewById(R.id.true_button);
        Button false_button = findViewById(R.id.false_button);
        Button next_button = findViewById(R.id.next_button);
        Button cheat_button = findViewById(R.id.cheat_button);

        true_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(),question_answer[question_index[0]][0],Toast.LENGTH_SHORT).show();
            }
        });

        false_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(),question_answer[question_index[0]][1],Toast.LENGTH_SHORT).show();
            }
        });

        next_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                question_index[0] = (question_index[0] +1)%6;
                question.setText(questions.get(question_index[0]));
            }
        });

        cheat_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAlertDialog();
            }
        });
    }

    private void showAlertDialog(){
        AlertDialog.Builder adBuilder = new AlertDialog.Builder(mContext);
        adBuilder.setTitle("对话框");
        adBuilder.setMessage("你确定要作弊吗");
        adBuilder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                showAlertDialog_answer();
            }
        });
        adBuilder.setNegativeButton("否",null);
        adBuilder.show();
    }

    private void showAlertDialog_answer(){
        AlertDialog.Builder adBuilder = new AlertDialog.Builder(mContext);
        adBuilder.setTitle("答案");
        adBuilder.setMessage(answer[question_index[0]]);
        adBuilder.setPositiveButton("确认",null);
        //adBuilder.setNegativeButton("取消",null);
        adBuilder.show();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值