【Android】控件与布局入门 - 简易计算器

目录

1. 基础开发环境

2. 计算器的布局和相关按钮

3. 计算器的主要运算逻辑

4. APK 文件

5. 项目源码


1. 基础开发环境

JDK:JDK17

Android Studio:Android Studio Giraffe | 2022.3.1

Android SDK:Android API 34

Gradle: gradle-8.0-bin.zip

2. 计算器的布局和相关按钮

使用 LinearLayout 和 GridLayout 实现计算器的交互前端。

layout 文件如下

<?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:background="#EEEEEE"android:orientation="vertical"android:padding="5dp"><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="@string/simple_calculator"android:textColor="@color/black"android:textSize="20sp"/><TextViewandroid:id="@+id/tv_result"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/white"android:lines="3"android:text="@string/filler"android:gravity="end|bottom"android:textColor="@color/black"android:textSize="25sp"/><GridLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:columnCount="4"android:rowCount="5"><Buttonandroid:id="@+id/btn_clear_entry"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/cancel"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_divide"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/divide"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_multiply"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/multiply"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_clear"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/delete"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_seven"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/seven"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_eight"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/eight"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_nine"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/nine"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_plus"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/plus"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_four"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/four"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_five"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/five"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_six"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/six"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_minus"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/minus"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_one"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/one"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_two"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/two"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_three"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/three"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_sqrt"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/sqrt"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_reciprocal"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/reciprocal"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_zero"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/zero"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_dot"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/dot"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/><Buttonandroid:id="@+id/btn_equal"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/equal"android:textColor="@color/black"android:textSize="@dimen/button_font_size"/></GridLayout></LinearLayout></ScrollView></LinearLayout>

相关 values 如下:

  • strings.xml
<resources><string name="app_name">calculator</string><string name="simple_calculator">计算器</string><string name="filler">0</string><string name="cancel">CE</string><string name="divide">÷</string><string name="multiply">×</string><string name="plus">+</string><string name="minus">-</string><string name="delete">C</string><string name="zero">0</string><string name="one">1</string><string name="two">2</string><string name="three">3</string><string name="four">4</string><string name="five">5</string><string name="six">6</string><string name="seven">7</string><string name="eight">8</string><string name="nine">9</string><string name="sqrt">√</string><string name="dot">.</string><string name="equal">=</string><string name="reciprocal">1/X</string>
</resources>
  • dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources><dimen name="button_font_size">30sp</dimen><dimen name="button_height">75dp</dimen>
</resources>

实际效果:

 

3. 计算器的主要运算逻辑

package com.example.calculator;import android.os.Bundle;
import android.view.View;
import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import java.util.Arrays;
import java.util.List;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView tvResult;private String firstNum = "";private String secondNum = "";private String operator = "";private String result = "";private String showText = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 从布局文件中获取文本视图tvResult = findViewById(R.id.tv_result);// 为所有按钮注册点击监听器findViewById(R.id.btn_clear_entry).setOnClickListener(this);findViewById(R.id.btn_divide).setOnClickListener(this);findViewById(R.id.btn_multiply).setOnClickListener(this);findViewById(R.id.btn_clear).setOnClickListener(this);findViewById(R.id.btn_seven).setOnClickListener(this);findViewById(R.id.btn_eight).setOnClickListener(this);findViewById(R.id.btn_nine).setOnClickListener(this);findViewById(R.id.btn_plus).setOnClickListener(this);findViewById(R.id.btn_four).setOnClickListener(this);findViewById(R.id.btn_five).setOnClickListener(this);findViewById(R.id.btn_six).setOnClickListener(this);findViewById(R.id.btn_minus).setOnClickListener(this);findViewById(R.id.btn_one).setOnClickListener(this);findViewById(R.id.btn_two).setOnClickListener(this);findViewById(R.id.btn_three).setOnClickListener(this);findViewById(R.id.btn_sqrt).setOnClickListener(this);findViewById(R.id.btn_reciprocal).setOnClickListener(this);findViewById(R.id.btn_zero).setOnClickListener(this);findViewById(R.id.btn_dot).setOnClickListener(this);findViewById(R.id.btn_equal).setOnClickListener(this);}@Overridepublic void onClick(View view) {String inputText = ((TextView) view).getText().toString();List<Integer> fourOperations = Arrays.asList(R.id.btn_plus, R.id.btn_minus, R.id.btn_multiply, R.id.btn_divide);int id = view.getId();if (id == R.id.btn_clear) {// 清除clear();} else if (id == R.id.btn_clear_entry) {// 清除输入clearEntryOperate();} else if (fourOperations.contains(id)) {operator = inputText;refreshShowText(showText + operator);// 四则运算} else if (id == R.id.btn_equal) {// 等号if (!secondNum.equals("")) {double calculateResult = calculateFour();refreshOperateResult(String.valueOf(calculateResult));refreshShowText(result);}} else if (id == R.id.btn_sqrt) {// 根号double sqrtResult = Math.sqrt(Double.parseDouble(firstNum));refreshOperateResult(String.valueOf(sqrtResult));refreshShowText(result);} else if (id == R.id.btn_reciprocal) {// 倒数double reciprocalResult = 1.0 / Double.parseDouble(firstNum);refreshOperateResult(String.valueOf(reciprocalResult));refreshShowText(result);} else {// 其它if (result.length() > 0 && operator.equals("")) {clear();}if (operator.equals("")) {firstNum += inputText;} else {secondNum += inputText;}if (showText.equals("0") && !inputText.equals(".")) {refreshShowText(inputText);} else {refreshShowText(showText + inputText);}}}private double calculateFour() {switch (operator) {case "+":return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);case "-":return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);case "×":return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);default:return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);}}private void clear() {refreshOperateResult("");refreshShowText("0");}private void refreshOperateResult(String newResult) {result = newResult;firstNum = result;secondNum = "";operator = "";}private void clearEntryOperate() {String tmp = showText.substring(0, showText.length() - 1);if (tmp.equals("")) {tmp = "0";}refreshShowText(tmp);}// 刷新显示文本private void refreshShowText(String text) {showText = text;integerRemoveDot();tvResult.setText(showText);}private void integerRemoveDot() {int start = showText.indexOf(".");if (start != -1) {int end = showText.length();String value = showText.substring(start + 1, end);if (Double.parseDouble(value) == 0.0) {showText = showText.substring(0, start);}}}
}

4. APK 文件

构建好的 APK 文件见文件开头,可直接下载安装。

5. 项目源码

https://gitee.com/hl0929/calculator

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/54501.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL5.7源码编译Debug版本

编译环境Ubuntu22.04LTS 1 官方下载MySQL源码 https://dev.mysql.com/downloads/mysql/?spma2c6h.12873639.article-detail.4.68e61a14ghILh5 2 安装基础软件 cmakeclangpkg-configperl 参考&#xff1a;https://dev.mysql.com/doc/refman/5.7/en/source-installation-prere…

LeetCode-Java(05)

19. 删除链表的倒数第 N 个结点 两个方法&#xff0c;方法一是先走一遍链表得出链表长度&#xff0c;再走第二遍&#xff0c;找到倒数第n个数。方法二是双指针&#xff0c;首先快指针就比慢指针多走n步&#xff0c;然后这俩指针同步走&#xff0c;快指针走到头了&#xff0c;慢…

uni-app、H5实现瀑布流效果封装,列可以自定义

文章目录 前言一、效果二、使用代码三、核心代码总结 前言 最近做项目需要实现uni-app、H5实现瀑布流效果封装&#xff0c;网上搜索有很多的例子&#xff0c;但是代码都是不够完整的&#xff0c;下面来封装一个uni-app、H5都能用的代码。在小程序中&#xff0c;一个个item渲染…

redis缓存雪崩和缓存击穿

目录 缓存雪崩 解决方案&#xff1a; 缓存击穿 ​解决方案 缓存雪崩 缓存雪崩是指在同一时段大量的缓存key同时失效或者Redis服务宕机&#xff0c;导致大量请求到达数据库&#xff0c;带来巨大压力。 解决方案&#xff1a; u 给不同的 Key 的 TTL 添加随机值 u 利用 Redis …

【黑马头条之kafka及异步通知文章上下架】

本笔记内容为黑马头条项目的kafka及异步通知文章上下架部分 目录 一、kafka概述 二、kafka安装配置 三、kafka入门 四、kafka高可用设计 1、集群 2、备份机制(Replication&#xff09; 五、kafka生产者详解 1、发送类型 2、参数详解 六、kafka消费者详解 1、消费者…

C++派生类的构造函数

1.构造函数 定义了派生类之后&#xff0c;要使用派生类就需要声明该类的对象。对象在使用之前必须初始化。 派生类的成员对象是由所有基类的成员对象共同组成的。因此构造派生类函数的对象时&#xff0c;就要对基类的成员对象和新增的成员对象进行初始化。 基类的构造函数并…

Linux CentOS系统怎么下载软件

Linux CenOS系统想要下载软件可以在Linux内置的应用商店&#xff0c;并通过Yum 包管理器来下载&#xff08;直接使用yum命令下载软件&#xff09; 在Linux系统中&#xff0c;Yum&#xff08;Yellowdog Updater, Modified&#xff09;是用于管理RPM软件包的一个包管理器。 安装…

ChatGPT-4.0:你准备好了吗?

3202年了&#xff0c;你还在用ChatGPT 3.5吗&#xff1f; 来感受一下ChatGPT 4.0的魅力吧 文末附升级链接 1、颠倒黑白&#xff1f; 2、解读幽默&#xff1f; 3、小镇做题家&#xff1f; 如何白嫖&#xff1f; 最后 1、科技的发展加快了知识更新的速度&#xff0c;唯有终身…

数据库操作系列-Mysql, Postgres常用sql语句总结

文章目录 1.如果我想要写一句sql语句&#xff0c;实现 如果存在则更新&#xff0c;否则就插入新数据&#xff0c;如何解决&#xff1f;MySQL数据库实现方案: ON DUPLICATE KEY UPDATE写法 Postgres数据库实现方案:方案1&#xff1a;方案2&#xff1a;关于更新&#xff1a;如何实…

前端实现打印1 - 使用 iframe 实现 并 分页打印

目录 打印代码对话框预览打印预览 打印代码 <!-- 打印 --> <template><el-dialogtitle"打印":visible.sync"dialogVisible"width"50%"top"7vh"append-to-bodyclose"handleClose"><div ref"print…

大数据与okcc呼叫中心融合的几种方式

在实际的生产实践中&#xff0c;为提高营销效率&#xff0c;避免骚扰大众&#xff0c;很多呼叫中心业务会与大数据平台进行合作&#xff0c;进行精准营销。 买卖数据是非法的&#xff0c;大数据平台方并不会提供直接的数据&#xff0c;一般情况下&#xff0c;提供的数据都是脱…

C#利用自定义特性以及反射,来提大型项目的开发的效率

在大型项目的开发过程中&#xff0c;需要多人协同工作&#xff0c;来加速项目完成进度。 比如一个软件有100个form&#xff0c;分给100个人来写&#xff0c;每个人完成自己的Form.cs的编写之后&#xff0c;要在Mainform调用自己写的Form。 如果按照正常的Form form1 new For…