一、实验目的
- 学习和掌握Android开发的基础知识,包括Activity、控件和布局等。
- 通过实际编写计算器应用,提高Android应用开发的实践能力。
二、实验环境
- Android Studio开发环境
- Kotlin编程语言
三、实验内容
- 创建一个新的Android项目,并设置基本的项目结构和属性。
- 设计计算器的用户界面,包括布局和控件的添加。
- 实现计算器的基本功能,如数字输入、运算符选择以及计算结果展示。
四、实验步骤
步骤一:创建新的Android项目
- 打开Android Studio,选择“创建新项目”。
- 填写项目名称、保存位置等信息,选择最小SDK版本。
- 选择“Empty Activity”作为项目模板,点击“Finish”创建项目。
步骤二:设计计算器的用户界面
- 打开
activity_main.xml
文件,这是主Activity的布局文件。0 - 使用LinearLayout或RelativeLayout等布局管理器来组织控件。
- 添加必要的控件,如Button(用于数字和运算符)、EditText(用于显示计算结果)等。
- 设置控件的属性,如id、文本内容、背景等。
<Button>:这个标签表示创建一个按钮元素。
android:id="@+id/button_toggle_sign":这个属性为按钮指定一个唯一的标识符。在应用程序的其他地方,可以使用这个标识符来引用这个按钮。
android:layout_width="0dp":这个属性定义了按钮在布局中的宽度。这里设置为0dp,这种技巧通常用于实现权重(weight)布局,表示按钮的宽度将由其他具有权重属性的元素共享。
android:layout_height="wrap_content":这个属性定义了按钮在布局中的高度。设置为wrap_content表示按钮的高度会根据按钮中的内容自动调整。
android:layout_weight="1":这个属性定义了按钮在布局中的权重。在此布局中,按钮的宽度会根据权重进行分配。这里的值为1,表示按钮会占据剩余空间的相等部分,因为这个按钮与其他具有权重属性的元素共享宽度。
android:text="±":这个属性定义了按钮上显示的文本内容,即按钮上的文本为"±",它通常表示切换正负号的功能。
最终布局效果如下:
部分源码
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
/>
<RelativeLayout
android:background="#c0c0ff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="@android:color/white"
android:hint="0"
android:textSize="24sp"
android:textAlignment="textEnd"
android:padding="16dp"
android:editable="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editText_result"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_backspace"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="←"/>
<Button
android:id="@+id/button_toggle_sign"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="±"/>
<Button
android:id="@+id/button_dot"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."/>
<Button
android:id="@+id/button_percent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="%"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"/>
<Button
android:id="@+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"/>
<Button
android:id="@+id/button_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"/>
<Button
android:id="@+id/button_divide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"/>
<Button
android:id="@+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"/>
<Button
android:id="@+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"/>
<Button
android:id="@+id/button_multiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"/>
<Button
android:id="@+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"/>
<Button
android:id="@+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"/>
<Button
android:id="@+id/button_minus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"/>
<Button
android:id="@+id/button_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"/>
<Button
android:id="@+id/button_equal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="/>
<Button
android:id="@+id/button_plus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"/>
</LinearLayout>
</LinearLayout>
<!--
<TextView
android:text="20212329cpy"
android:textSize="45dp"
android:layout_marginTop="350dp"
android:layout_marginLeft="30dp"
android:layout_width="330dp"
android:layout_height="130dp"
android:id="@+id/logo"
android:rotation="-10"/>
-->
</RelativeLayout>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
步骤三:实现计算器的基本功能
- 打开
MainActivity.kt
(如果使用Kotlin)或MainActivity.java
(如果使用Java)文件。 - 为每个按钮控件设置点击事件监听器。
override fun onClick(v: View) { var str = et_input!!.text.toString() val id = v.id if (id == R.id.btn_0 || id == R.id.btn_1 || id == R.id.btn_2 || id == R.id.btn_3 || id == R.id.btn_4 || id == R.id.btn_5 || id == R.id.btn_6 || id == R.id.btn_7 || id == R.id.btn_8 || id == R.id.btn_9 || id == R.id.btn_pt) { if (clr_flag) { clr_flag = false str = "" et_input!!.setText("") } et_input!!.setText(str + (v as Button).text) } else if (id == R.id.btn_add || id == R.id.btn_sub || id == R.id.btn_mul || id == R.id.btn_div) { if (clr_flag) { clr_flag = false str = "" et_input!!.setText("") } if (str.contains("+") || str.contains("-") || str.contains("×") || str.contains("÷")) { str = str.substring(0, str.indexOf(" ")) } et_input!!.setText(str + " " + (v as Button).text + " ") } else if (id == R.id.btn_clr) { if (clr_flag) clr_flag = false str = "" et_input!!.setText("") } else if (id == R.id.btn_del) { if (clr_flag) { clr_flag = false str = "" et_input!!.setText("") } else if (str != null && str != "") { et_input!!.setText(str.substring(0, str.length - 1)) } } else if (id == R.id.btn_eq) { result } }
- 在监听器的回调方法中实现计算逻辑,根据用户输入的数字和运算符进行计算,并将结果显示在EditText控件中。
五、示例代码
以下是使用Kotlin编写的简单计算器应用的示例代码片段:
package com.example.a20212301gzy
import android.R
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(), View.OnClickListener {
var btn_0: Button? = null
var btn_1: Button? = null
var btn_2: Button? = null
var btn_3: Button? = null
var btn_4: Button? = null
var btn_5: Button? = null
var btn_6: Button? = null
var btn_7: Button? = null
var btn_8: Button? = null
var btn_9: Button? = null
var btn_pt: Button? = null
var btn_mul: Button? = null
var btn_div: Button? = null
var btn_add: Button? = null
var btn_sub: Button? = null
var btn_clr: Button? = null
var btn_del: Button? = null
var btn_eq: Button? = null
var et_input: EditText? = null
var clr_flag = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setContentView(R.layout.activity_main)
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_pt = findViewById<View>(R.id.btn_pt) as Button
btn_add = findViewById<View>(R.id.btn_add) as Button
btn_sub = findViewById<View>(R.id.btn_sub) as Button
btn_mul = findViewById<View>(R.id.btn_mul) as Button
btn_div = findViewById<View>(R.id.btn_div) as Button
btn_clr = findViewById<View>(R.id.btn_clr) as Button
btn_del = findViewById<View>(R.id.btn_del) as Button
btn_eq = findViewById<View>(R.id.btn_eq) as Button
et_input = findViewById<View>(R.id.et_input) as EditText
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_pt!!.setOnClickListener(this)
btn_add!!.setOnClickListener(this)
btn_sub!!.setOnClickListener(this)
btn_mul!!.setOnClickListener(this)
btn_div!!.setOnClickListener(this)
btn_clr!!.setOnClickListener(this)
btn_del!!.setOnClickListener(this)
btn_eq!!.setOnClickListener(this)
}
override fun onClick(v: View) {
var str = et_input!!.text.toString()
val id = v.id
if (id == R.id.btn_0 || id == R.id.btn_1 || id == R.id.btn_2 || id == R.id.btn_3 || id == R.id.btn_4 || id == R.id.btn_5 || id == R.id.btn_6 || id == R.id.btn_7 || id == R.id.btn_8 || id == R.id.btn_9 || id == R.id.btn_pt) {
if (clr_flag) {
clr_flag = false
str = ""
et_input!!.setText("")
}
et_input!!.setText(str + (v as Button).text)
} else if (id == R.id.btn_add || id == R.id.btn_sub || id == R.id.btn_mul || id == R.id.btn_div) {
if (clr_flag) {
clr_flag = false
str = ""
et_input!!.setText("")
}
if (str.contains("+") || str.contains("-") || str.contains("×") || str.contains("÷")) {
str = str.substring(0, str.indexOf(" "))
}
et_input!!.setText(str + " " + (v as Button).text + " ")
} else if (id == R.id.btn_clr) {
if (clr_flag) clr_flag = false
str = ""
et_input!!.setText("")
} else if (id == R.id.btn_del) {
if (clr_flag) {
clr_flag = false
str = ""
et_input!!.setText("")
} else if (str != null && str != "") {
et_input!!.setText(str.substring(0, str.length - 1))
}
} else if (id == R.id.btn_eq) {
result
}
}
private val result: Unit
private get() {
val exp = et_input!!.text.toString()
if (exp == null || exp == "") return
if (!exp.contains(" ")) {
return
}
if (clr_flag) {
clr_flag = false
return
}
clr_flag = true
val s1 = exp.substring(0, exp.indexOf(" "))
val op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2)
val s2 = exp.substring(exp.indexOf(" ") + 3)
var cnt = 0.0
if (s1 != "" && s2 != "") {
val d1 = s1.toDouble()
val d2 = s2.toDouble()
if (op == "+") {
cnt = d1 + d2
}
if (op == "-") {
cnt = d1 - d2
}
if (op == "×") {
cnt = d1 * d2
}
if (op == "÷") {
cnt = if (d2 == 0.0) 0.0 else d1 / d2
}
if (!s1.contains(".") && !s2.contains(".") && op != "÷") {
val res = cnt.toInt()
et_input!!.setText(res.toString() + "")
} else {
et_input!!.setText(cnt.toString() + "")
}
} else if (s1 != "" && s2 == "") {
val d1 = s1.toDouble()
if (op == "+") {
cnt = d1
}
if (op == "-") {
cnt = d1
}
if (op == "×") {
cnt = 0.0
}
if (op == "÷") {
cnt = 0.0
}
if (!s1.contains(".")) {
val res = cnt.toInt()
et_input!!.setText(res.toString() + "")
} else {
et_input!!.setText(cnt.toString() + "")
}
} else if (s1 == "" && s2 != "") {
val d2 = s2.toDouble()
if (op == "+") {
cnt = d2
}
if (op == "-") {
cnt = 0 - d2
}
if (op == "×") {
cnt = 0.0
}
if (op == "÷") {
cnt = 0.0
}
if (!s2.contains(".")) {
val res = cnt.toInt()
et_input!!.setText(res.toString() + "")
} else {
et_input!!.setText(cnt.toString() + "")
}
} else {
et_input!!.setText("")
}
}
}
六、实验总结
对于Kotlin:Kotlin作为Java的改良,在Android开发中有很多优势,通过这次实验我相对直观感受到了其优势所在,从UI界面绘制开始,到使用findViewById去绑定xml布局中的控件,很多操作非常便捷,不需要有多深层次的编程思想,易上手好掌握。但是本次实验也只是对于Kotlin浅尝辄止的试炼,还要多多练习,熟练与java不同的地方,还有anko框架的使用等。
对于Android开发:Android开发涉及到很多框架和工具,在理解好了这些工具之后让它们变得“趁手”可以更加高效地开发,一是要掌握好课堂上的基础知识,Android四大组件的生命周期和使用方法,掌握如何使用好gradle,管理项目依赖、配置构建选项。二是要多多在实践编写中越挫越勇。