20212408 2023-2024-2 《移动平台开发与实践》第2次作业

20212408 2023-2024-2 《移动平台开发与实践》第2次作业

1.实验内容

内容回顾:

上周的学习中提到了四大组件,分别是Activity、Service、BroadcastReceiver和ContentProvider,每个组件都有自己的特点和作用:
Activity(活动):Activity是用户界面的基本单元,通常表示一个屏幕上的窗口,负责与用户进行交互。每个Activity都是一个独立的类,可以包含布局和控件,用来展示界面和响应用户操作。Activity之间可以通过Intent进行通信和切换。
Service(服务):Service是在后台执行长时间运行操作的组件,它没有用户界面,可以在后台播放音乐、下载文件、处理网络请求等。Service可以与Activity进行通信,让应用在后台执行一些任务而不影响用户的操作。
BroadcastReceiver和ContentProvider尚未开始学习。

实验内容:

  • 创建一个新的Android项目:可以使用Android Studio创建一个新的项目,选择合适的项目模板和设置,创建基本的项目结构和属性。

  • 设计计算器的用户界面:使用布局来设计计算器的界面,添加所需的控件(如按钮、文本框等),并设置它们的位置和大小。

  • 实现计算器的基本功能:编写代码实现数字输入、运算符选择以及计算结果展示等功能。可以使用Java语言来编写计算器应用的逻辑代码。

2.实验过程

2.1创建新的Android项目

打开Android Studio,选择“创建新项目”。
填写项目名称、保存位置等信息,选择最小SDK版本。
选择“Empty Activity”作为项目模板,点击“Finish”创建项目。
请添加图片描述

2.2 设计计算器的用户界面

打开activity_main.xml文件,这是主Activity的布局文件。
使用LinearLayout或RelativeLayout等布局管理器来组织控件。
添加必要的控件,如Button(用于数字和运算符)、EditText(用于显示计算结果)等。
设置控件的属性,如id、文本内容、背景等。
这里我们根据需求设置0~9共计十个数字控件和加减乘除和清除等运算控件。
主要用到的布局如下:

  • 线性布局LinearLayout:因为计算器界面整体从上往下布局,所以需要垂直方向的LinearLayout。
  • 文本视图TextView:很明显顶部标题“简单计算器”就是TextView,且文字居中显示;标题下面的计算结果也需要使用TextView,且文字靠右靠下显示。
  • 按钮Button:几乎所有的数字与运算符按钮都采用了Button控件。
    请添加图片描述
    控件设置的情况如图所示,可以使用直接拖动调整或者代码输入的方式进行控件的操作。

2.3:实现计算器的基本功能

打开MainActivity.kt(如果使用Kotlin)
为每个按钮控件设置点击事件监听器。
在监听器的回调方法中实现计算逻辑,根据用户输入的数字和运算符进行计算,并将结果显示在EditText控件中。
编写MainActivity.kt实现计算器效果:
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
并且可以在AS上将apk给下载下来,然后在手机上进行下载安装,然后同样也能实现计算机的功能。
实现结果如图:
请添加图片描述

实验代码:

  • activity_main.xml
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="438dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/top"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:maxLines="2"
            android:textSize="30sp"
            android:text="20212408张圣杰简易计算器"
            android:textColor="#8B0A50"
            >
        </TextView>
        <TextView
            android:id="@+id/msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:maxLines="2"
            android:textSize="30sp"
            android:background="#F2F2F2"
            >
        </TextView>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Ac"
            android:id="@+id/btn_ac"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="("
            android:id="@+id/btn_left"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text=")"
            android:id="@+id/btn_right"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Del"
            android:id="@+id/btn_del"
            >

        </Button>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7"
            android:id="@+id/btn_7"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="8"
            android:id="@+id/btn_8"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9"
            android:id="@+id/btn_9"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+"
            android:id="@+id/btn_plus"
            >

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

        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4"
            android:id="@+id/btn_4"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5"
            android:id="@+id/btn_5"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6"
            android:id="@+id/btn_6"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-"
            android:id="@+id/btn_sub"
            >

        </Button>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3"
            android:id="@+id/btn_3"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2"
            android:id="@+id/btn_2"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"
            android:id="@+id/btn_1"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="*"
            android:id="@+id/btn_multi"
            >

        </Button>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="/"
            android:id="@+id/btn_div"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="0"
            android:id="@+id/btn_0"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="."
            android:id="@+id/btn_point"
            >

        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="="
            android:id="@+id/btn_equals"
            >

        </Button>
    </LinearLayout>
</LinearLayout>
  • MainActivity.kt
MainActivity.kt

package com.example.calculator

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.example.calculator.IndexInToDuffix.Houzhui
import java.util.*

class MainActivity : AppCompatActivity(), View.OnClickListener {
    private var btn_0: Button? = null
    private var btn_1: Button? = null
    private var btn_2: Button? = null
    private var btn_3: Button? = null
    private var btn_4: Button? = null
    private var btn_5: Button? = null
    private var btn_6: Button? = null
    private var btn_7: Button? = null
    private var btn_8: Button? = null
    private var btn_9: Button? = null
    private var btn_point: Button? = null
    private var btn_plus: Button? = null
    private var btn_sub: Button? = null
    private var btn_multi: Button? = null
    private var btn_div: Button? = null
    private var btn_left: Button? = null
    private var btn_right: Button? = null

    private var btn_ac: Button? = null  //清空
    private var btn_del: Button? = null//删除
    private var btn_equals: Button? = null  //等号
    private var et_showview: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    fun initView() {
        btn_0 = findViewById<View>(R.id.btn_0) as Button
        btn_1 = findViewById<View>(R.id.btn_1) as Button
        btn_2 = findViewById<View>(R.id.btn_2) as Button
        btn_3 = findViewById<View>(R.id.btn_3) as Button
        btn_4 = findViewById<View>(R.id.btn_4) as Button
        btn_5 = findViewById<View>(R.id.btn_5) as Button
        btn_6 = findViewById<View>(R.id.btn_6) as Button
        btn_7 = findViewById<View>(R.id.btn_7) as Button
        btn_8 = findViewById<View>(R.id.btn_8) as Button
        btn_9 = findViewById<View>(R.id.btn_9) as Button
        btn_point = findViewById<View>(R.id.btn_point) as Button
        btn_div = findViewById<View>(R.id.btn_div) as Button
        btn_plus = findViewById<View>(R.id.btn_plus) as Button
        btn_sub = findViewById<View>(R.id.btn_sub) as Button
        btn_multi = findViewById<View>(R.id.btn_multi) as Button

        btn_left = findViewById<View>(R.id.btn_left) as Button
        btn_right = findViewById<View>(R.id.btn_right) as Button
        btn_equals = findViewById<View>(R.id.btn_equals) as Button

        btn_ac = findViewById<View>(R.id.btn_ac) as Button
        btn_del = findViewById<View>(R.id.btn_del) as Button


        btn_0!!.setOnClickListener(this)
        btn_1!!.setOnClickListener(this)
        btn_2!!.setOnClickListener(this)
        btn_3!!.setOnClickListener(this)
        btn_4!!.setOnClickListener(this)
        btn_5!!.setOnClickListener(this)
        btn_6!!.setOnClickListener(this)
        btn_7!!.setOnClickListener(this)
        btn_8!!.setOnClickListener(this)
        btn_9!!.setOnClickListener(this)
        btn_equals!!.setOnClickListener(this)
        btn_del!!.setOnClickListener(this)
        btn_right!!.setOnClickListener(this)
        btn_left!!.setOnClickListener(this)
        btn_multi!!.setOnClickListener(this)
        btn_sub!!.setOnClickListener(this)
        btn_plus!!.setOnClickListener(this)
        btn_div!!.setOnClickListener(this)
        btn_ac!!.setOnClickListener(this)
        btn_point!!.setOnClickListener(this)
        et_showview = findViewById<View>(R.id.msg) as TextView
    }

    override fun onClick(view: View) {
        var str = et_showview!!.text.toString()

        when (view.id) {
            R.id.btn_0 -> {
                str += "0"
                et_showview!!.text = str
            }
            R.id.btn_1 -> {
                str += "1"
                et_showview!!.text = str
            }
            R.id.btn_2 -> {
                str += "2"
                et_showview!!.text = str
            }
            R.id.btn_3 -> {
                str += "3"
                et_showview!!.text = str
            }
            R.id.btn_4 -> {
                str += "4"
                et_showview!!.text = str
            }
            R.id.btn_5 -> {
                str += "5"
                et_showview!!.text = str
            }
            R.id.btn_6 -> {
                str += "6"
                et_showview!!.text = str
            }
            R.id.btn_7 -> {
                str += "7"
                et_showview!!.text = str
            }
            R.id.btn_8 -> {
                str += "8"
                et_showview!!.text = str
            }
            R.id.btn_9 -> {
                str += "9"
                et_showview!!.text = str
            }
            R.id.btn_div -> if (str.length != 0) {
                str += "/"
                et_showview!!.text = str
            }

            R.id.btn_multi -> if (str.length != 0) {
                str += "*"
                et_showview!!.text = str
            }
            R.id.btn_point -> if (str.length != 0 && str[str.length - 1] >= '0' && str[str.length - 1] <= '9') {
                str += "."
                et_showview!!.text = str
            }
            R.id.btn_plus -> if (str.length != 0) {
                str += "+"
                et_showview!!.text = str
            }
            R.id.btn_sub -> if (str.length != 0) {
                str += "-"
                et_showview!!.text = str
            }
            //如果运算符和小数点不出现在首尾位置,可以添加进字符串里
            //小数点前一位必须是数字
            R.id.btn_left -> {
                str += "("
                et_showview!!.text = str
            }
            R.id.btn_right -> {
                str += ")"
                et_showview!!.text = str
            }
            R.id.btn_equals ->
                et_showview!!.text = IndexInToDuffix.calc(Houzhui(str))//输入结束,转为逆波兰表达式
            R.id.btn_del -> {
                str = str.substring(0, str.length - 1)
                et_showview!!.text = str
            }
            R.id.btn_ac -> {
                str = ""
                et_showview!!.text = str
            }
        }//如果字符串长度大于等于1,就前删一个字符
    }
}
  • AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.calculator">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3.学习中遇到的问题及解决

问题1:编写代码完成后虚拟机无法运行

  • 问题1解决方案
    总是弹出提示:
    Pixel_3a_API_34_extension_level_7_x86_64 已在运行。如果不是这种情况,请删除 C:\Users\十方.android\avd\Pixel_3a_API_34_extension_level_7_x86_64.avd*.lock 并重试。
    开始我认为是进程占用,于是选择重启AS并运行,发现并没有解决,于是删除虚拟机,重新安装虚拟机,就能正常运行了,对此表示很疑惑,仍然不知道具体的原因。

问题2:activity出现报错

请添加图片描述

  • 问题2解决方案
    点击左侧的红色灯泡,会弹出修改的建议,点击会进行自动的修改补充。
    上网搜索后得出:从 Android 12 开始,必须设置 android:exported;使用 true 使该活动可供其他应用程序使用,否则使用 false。对于启动器活动,应将其设置为 true。

问题3:对于一些非法运算没有很好的处理

  • 问题3解决方案
    在运算除法除以0时会出现运算问题闪退,这里开始我是没有去管,后来想到上课学的toast的操作,由于上网搜索并结合本次实现实现了如果是非法的运算会弹出toast提示。
    如下图:
    请添加图片描述
    代码:
R.id.btn_equals ->
                try {
                    val result = IndexInToDuffix.calc(Houzhui(str))
                    et_showview!!.text = result
                    lastCalculation = result
                } catch (e: Exception) {
                    Toast.makeText(this, "运算错误", Toast.LENGTH_SHORT).show()
                    lastCalculation = null
                    et_showview!!.text = ""
                }

4.学习感悟、思考等

在完成Android开发的计算器应用实验后,我对代码编写方面有了许多收获和体会。
在设计计算器应用的用户界面时,我学会了如何使用XML布局文件来定义界面的结构和控件的位置。通过使用LinearLayout和RelativeLayout等布局管理器,我可以灵活地排列各种控件,实现不同的界面效果。这让我对Android界面布局有了更深入的理解,也提高了我的布局设计能力。
最重要的是,在实现计算器应用的基本功能时,我学会了如何编写Kotlin代码来处理用户的输入和计算结果。我使用了EditText控件来显示用户输入的数字和计算结果,使用Button控件来响应用户的点击事件,并通过编写逻辑代码来实现数字输入、运算符选择和计算结果展示等功能。这让我对Kotlin语言的运用和Android开发中的事件处理有了更深入的理解。
在编写代码的过程中,我遇到了一些问题,比如如何处理用户输入的表达式、如何正确地计算结果等。通过查阅资料和思考,我逐渐找到了解决方案,并且学会了如何利用调试工具来调试代码,定位和解决问题。这让我对问题的解决有了更多的信心,也提高了我的问题分析和解决能力。
总的来说,通过这次实验,我不仅加深了对Android开发的理解,也提高了我的实践能力和编码能力。我相信这些经验和技能在今后的学习和工作中会给我带来很大的帮助,我也会继续学习和探索Android开发的更多知识,不断提升自己的技术水平。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值