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

这篇具有很好参考价值的文章主要介绍了【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">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="@string/simple_calculator"
                android:textColor="@color/black"
                android:textSize="20sp"/>

            <TextView
                android: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"/>

            <GridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnCount="4"
                android:rowCount="5">

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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"/>

                <Button
                    android: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>

实际效果:

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

 

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 = "";

    @Override
    protected 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);
    }

    @Override
    public 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文章来源地址https://www.toymoban.com/news/detail-623231.html

到了这里,关于【Android】控件与布局入门 - 简易计算器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 学会使用Android Studio网格布局制作计算器界面

    1.1GridLayout布局使用虚细线将布局划分为行、列和单元格,也支持一个控件在行、列上都有交错排列。 1.2可以自己设置布局中组件的排列方式 1.3可以自定义网格布局有多少行、多少列 1.4可以直接设置组件位于某行某列 1.5可以设置组件横跨几行或者几列 基于-  Empty Activity 模板

    2024年02月08日
    浏览(37)
  • android计算器界面布局线性布局跨2行,使用Kotlin高效地开发Android App(一,GitHub标星3.2K

    get(url).placeholder(R.drawable.shape_default_round_bg) .error(R.drawable.shape_default_round_bg) // .apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0))) .transform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0)) .into(this) } /** 占位符圆形 */ fun ImageView.loadCircle(url: Drawable) {

    2024年04月11日
    浏览(33)
  • Android Studio实现简易计算器(带横竖屏,深色浅色模式,更该按钮颜色,selector,style的使用)

    目录 前言 运行结果: 运行截屏(p50e)  apk文件 源码文件  项目结构 总览 MainActivity.java drawable 更改图标的方法: blackbutton.xml bluebuttons.xml greybutton.xml orangebuttons.xml whitebutton.xml layout 布局文件  竖屏: 横屏: values         colors.xml strings.xml styles 浅色模式 深色模式 themes.xml

    2024年02月06日
    浏览(35)
  • JAVA制作的简易计算器——傻瓜计算器

    用JAVA编写的傻瓜计算器 作用: 1.可以实现加法、减法、乘法、除法简单运算且是单一运算,不可混合使用。 2.CE为清除键 3.没有小数点O(∩_∩)O 思路: 创建JFrame窗口,设置标题,创建JButton,创建文本框JTextField用作显示。 先定义各种按钮类型,用作成员。定义窗口方法对窗口

    2024年02月11日
    浏览(33)
  • java 简易计算器

    1.使用Java图形界面组件设计软件,界面如图所示。 2.软件能够满足基本的“加、减、乘、除”等运算要求。 3.程序代码清晰,语法规范,结构合理,逻辑正确。 先分析,计算器大概是由三个大部分组成的:菜单栏,显示框,按钮。 所以定义一个类cal继承JFrame。 我们定义完后

    2024年02月01日
    浏览(41)
  • JAVA简易计算器

    1.C是清除键,功能是将之前所输入的数字、计算结果等信息全部归零 2.CE,清除当前输入的数据或符号 3.单击MS存储当前显示值,可以理解为放到存储区 4.单击MC清除存储区数值 5.单击MR将存储区数据显示到屏幕上 6.M+:当前显示的数与存储区的数相加 7.M-:当前显示的数与存储

    2024年02月09日
    浏览(40)
  • QT 简易计算器

    2024年02月09日
    浏览(31)
  • C# 制作简易计算器

    前言:环境是vs 2022 1、打开vs2022后,右边导航栏选择创建新项目。  2、选择Windows窗体应用(.net  Framework)  3、进入配置新项目界面(项目名称和位置可自行修改)点击创建  4、窗体From1即为我们要要编辑的位置  5、在窗体中添加对应的工具 6、并在对应的属性窗口为其修改

    2024年02月08日
    浏览(39)
  • Java计算器简易代码

    我写的计算器 网上搜的进阶版本 拿走不谢!

    2024年02月11日
    浏览(32)
  • MFC基于对话框——仿照Windows计算器制作C++简易计算器

    目录 一、界面设计 二、设置成员变量 三、初始化成员变量  四、初始化对话框 ​五、添加控件代码 1.各个数字的代码(0~9) 2.清除功能的代码 3.退格功能的代码 4.加减乘除功能的代码 5.小数点功能的代码 6.正负号功能的代码 7.等于功能的代码 六、源码领取方式 制作好之后

    2024年02月05日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包