Android Studio制作一个简单的计算器APP

这篇具有很好参考价值的文章主要介绍了Android Studio制作一个简单的计算器APP。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

虽然现在我们日常生活中很少用到计算器,但是第一次尝试在Android Studio上做一个计算器

程序设计步骤: (1)在布局文件中声明编辑文件框EditText,按钮Button等组件。 (2)在MainActivity中获取组件实例。 (3)通过swtich函数,判断输入的内容,并进行相应操作,通过getText()获取文本内容,setText()显示。

程序代码设计: 布局实现:在activi_main.xml中设置。使用线性布局(LinearLayout)与网格布局(GridLayout)来设置界面。在设计区域设置一个4行4列的网格布局,每行划分为均等的16个按钮,分别代表数字0-9,小数点,和运算符加减乘除以及等于号。  

最终效果图如下:

Android Studio制作一个简单的计算器APP文章来源地址https://www.toymoban.com/news/detail-508874.html

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"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="16dp"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="20dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/Answer"
        android:layout_width="match_parent"
        android:layout_height="189dp"
        android:layout_weight="0.33"
        android:hint="Result"
        android:textSize="34sp" />

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

        <Button
            android:id="@+id/btn10"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="C"
            android:textColor="#F30C4D"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn11"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="%"
            android:textColor="#FF5722"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn12"
            android:layout_width="175dp"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="Del"
            android:textColor="#F30C4D"
            android:textSize="20sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="1"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/teal_200"
            android:text="2"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="3"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btnadd"
            android:layout_width="wrap_content"
            android:layout_height="77dp"
            android:background="#00BCD4"
            android:text="+"
            android:textColor="#FFEB3B"
            android:textSize="26dp" />
    </LinearLayout>

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

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="4"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="5"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="6"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btnsub"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="-"
            android:textColor="#FFEB3B"
            android:textSize="26dp" />
    </LinearLayout>

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

        <Button
            android:id="@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="7"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="8"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn9"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="9"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btnmul"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/purple_200"
            android:text="×"
            android:textColor="#FFEB3B"
            android:textSize="26dp" />
    </LinearLayout>

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

        <Button
            android:id="@+id/btn0"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="0"
            android:textColor="#E91E63"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btndot"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="."
            android:textSize="26dp" />

        <Button
            android:id="@+id/btnequel"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="="
            android:textColor="#FF5722"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btndiv"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="÷"
            android:textColor="#FFEB3B"
            android:textSize="26dp" />
    </LinearLayout>


</LinearLayout>

java文件: 

package com.example.mycalculater;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bsub,bmul,bdiv,bdot,bequal,b10,b12;
    TextView ans;
    double var1,var2;
    boolean add,sub,mul,div,n10;
    boolean sq = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1= (Button) findViewById(R.id.btn1);
        b2= (Button) findViewById(R.id.btn2);
        b3= (Button) findViewById(R.id.btn3);
        b4= (Button) findViewById(R.id.btn4);
        b5= (Button) findViewById(R.id.btn5);
        b6= (Button) findViewById(R.id.btn6);
        b7= (Button) findViewById(R.id.btn7);
        b8= (Button) findViewById(R.id.btn8);
        b9= (Button) findViewById(R.id.btn9);
        b0= (Button) findViewById(R.id.btn0);
        badd= (Button) findViewById(R.id.btnadd);
        bsub= (Button) findViewById(R.id.btnsub);
        bmul= (Button) findViewById(R.id.btnmul);
        bdiv= (Button) findViewById(R.id.btndiv);
        bdot= (Button) findViewById(R.id.btndot);
        bequal= (Button) findViewById(R.id.btnequel);
        b10= (Button) findViewById(R.id.btn10);
        b12= (Button) findViewById(R.id.btn12);
        ans = (TextView) findViewById(R.id.Answer);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"1");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"1");

            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"2");
                    sq=false;
                }else
                     ans.setText(ans.getText()+"2");
            }
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"3");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"3");
            }
        });
        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"4");
                    sq=false;
            }else
                ans.setText(ans.getText()+"4");
            }
        });
        b5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"5");
                    sq=false;
                }else
                ans.setText(ans.getText()+"5");
            }
        });
        b6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"6");
                    sq=false;
                }else
                ans.setText(ans.getText()+"6");
            }
        });
        b7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"7");
                    sq=false;
                }else
                ans.setText(ans.getText()+"7");
            }
        });
        b8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"8");
                    sq=false;
                }else
                ans.setText(ans.getText()+"8");
            }
        });
        b9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true) {
                    ans.setText(null);
                    ans.setText(ans.getText()+"9");
                    sq=false;
                }else
                ans.setText(ans.getText()+"9");
            }
        });
        b0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"0");
                    sq=false;
                }else
                ans.setText(ans.getText()+"0");
            }
        });
        bdot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if(ans.getText().toString().contains("."))
                        ans.setText(ans.getText()+"");
                    else
                        ans.setText(ans.getText()+".");
                } catch (Exception e) {
                    ans.setText("出错");
                }
            }
        });
        badd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    add=true;
                    ans.setText(null);
                }catch (RuntimeException a){
                    ans.setText("错误");
                    sq=true;

                }
            }
        });
        bsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    sub=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("错误");
                    sq=true;
                }
            }
        });
        bmul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    mul=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("错误");
                    sq=true;
                }
            }
        });
        bdiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*var1 = Double.parseDouble(ans.getText()+"");
                div=true;
                ans.setText(null);*/
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    div=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("错误");
                    sq=true;
                }
            }
        });
        b10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ans.setText(null);
                add=false;sub=false;mul=false;div=false;
            }
        });
        b12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String temp = "";
                    temp = ans.getText().toString().substring(0, ans.length() - 1);
                    ans.setText(temp + "");
                }catch(RuntimeException a){
                    ans.setText("错误");
                }
            }
        });
        bequal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var2 = Double.parseDouble(ans.getText() + "");
                    if (add == true) {
                        ans.setText(var1 + var2 + "");
                        add = false;
                        sq = true;
                    }
                    if (sub == true) {
                        ans.setText(var1 - var2 + "");
                        sub = false;
                        sq = true;
                    }
                    if (mul == true) {
                        ans.setText(var1 * var2 + "");
                        mul = false;
                        sq = true;
                    }
                    if (div == true) {
                        ans.setText(var1 / var2 + "");
                        div = false;
                        sq = true;
                    }
                    if (n10 == true) {
                        ans.setText(0 + "");
                        n10 = false;
                        sq = true;
                    }
                }catch(RuntimeException a){
                }
                /*String temp="";
                temp=ans.getText().toString().substring(0,ans.length()-1);
                ans.setText(temp+"");*/
            }
        });

//        public void opratorCalc(String operatorNumber,String currentOprator)
//        {
//            if(TextUtils.isEmpty(lastOperators))
//            {
//                firstNumber = Double.parseDouble(operatorNumber);
//                return;
//            }






    }
}

到了这里,关于Android Studio制作一个简单的计算器APP的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • 初学编程 第一个小程序Android studio实现计算器功能

    源代码下载:https://gitee.com/zha-yingying/calculator.git 1.建立一个新的Layout,我这里使用的是GridLayout(网格布局),提取屏幕宽度(方便后面设置子控件的宽度)GridLayout的特点是自定义网格布局有几行几列,我们可以将自控件自定义放在第几行第几列。 2.建立一个新的textview(文本

    2023年04月14日
    浏览(56)
  • 移动开发作业三:使用Android studio来实现简单的计算器

    一.实验要求 结合所学内容利用Android studio来设计一个开发实例,这里去我选择做一个简易的计算器,可以初步实现加减乘除。 二.实验功能 该计算器与我们平常手机上的计算器一样,可以进行加减乘除操作。 三.实验过程 1.首先是关于计算器的布局 在layout文件下的drawable文件

    2024年02月02日
    浏览(38)
  • 用Java制作简单的计算器

    本篇文章主要是提供思路,当然文章末尾也又提供了源代码。 代码也是写了几天,重要的理解,不是直接复制粘贴就交作业了。 转载请注明出处,尊重作者劳动成果。 目录 界面的设计: 事件的响应: 计算: 详细代码如下:  总结: 要制作一个简单的计算器,首先就是对

    2024年02月08日
    浏览(39)
  • 用python制作一个简易计算器

    这是一个用Python制作简单计算器的教程。你可以根据需要进行更多的改进,例如添加其他运算符或功能。 首先,我们需要创建一个简单的用户界面,用于显示计算器的按键和结果。在Python中,我们可以使用 tkinter 库来创建图形用户界面。创建一个新的Python文件,并将其命名为

    2024年02月11日
    浏览(40)
  • 制作一个简易的计算器app

    github项目地址:https://github.com/13008451162/AndroidMoblieCalculator 笔者的Ui制作的制作的比较麻烦仅供参考,在这里使用了多个LinearLayout对屏幕进行了划分。不建议大家这样做最好使用GridLayout会更加快捷简单 笔者大致划分是这样的: 使用了四个大框,在第四个大框里面有多个小框

    2024年02月15日
    浏览(36)
  • Qt 制作一个简易的计算器

    1.通过UI界面封装出计算器的大致模型 进入设计页面后,左侧会有各种控件,可以将他们拖拽到你想编辑的窗口中,我们要做的是计算器,所以只用到很少几个控件,我们最主要用到Push Button这个控件来做我们计算器的按钮,lineEdit显示数字,我们可以将它拖拽到窗口,然后就

    2024年02月05日
    浏览(118)
  • Android Studio简易计算器

    目录 第一步,创建新项目 第二步,设计UI 第三步,实现计算逻辑 第四步,测试应用程序 随着移动互联网的普及,手机应用程序已经成为人们生活中不可或缺的一部分。计算器是一类被广泛使用的应用程序之一,因此学习如何开发一款简易的计算器应用程序是学习Android Stu

    2024年02月08日
    浏览(29)
  • 【Android Studio】简易计算器

    简易计算器要求: 1,操作简单,易于掌握,界面简单。 2.方便进行加,减,乘,除等操作。数字保留小数点后两位。 3.包含小数点运算和输入回退功能。 4.能够进行多次叠加运算。 5.系统能够进行多次叠加运算。 6.系统可以稳定运行。 功能图如下: 逻辑流程图如下: 项目建

    2024年02月08日
    浏览(35)
  • python界面开发案例:制作一个计算器软件

    前言 大家早好、午好、晚好吖 ❤ ~欢迎光临本文章 在我们手机上大家都有一个计算器,对吧 那它这功能是怎么实现的呢? 今天我们在电脑上来实现一个电脑端计算器界面~ 开发环境: Python 3.8 / 编译器 Pycharm 2021.2版本 / 编辑器 本文所有模块环境源码教程皆可点击文章下方

    2023年04月16日
    浏览(45)
  • Java中规模软件开发实训——简单计算器制作

    ✨ 博主: 命运之光 🌸 专栏: Python星辰秘典 🐳 专栏: web开发(html css js) ❤️ 专栏: Java经典程序设计 ☀️ 博主的其他文章: 点击进入博主的主页 前言: 在现代社会中,计算器是我们生活中不可或缺的工具之一。它们可以轻松地进行各种数值计算,从简单的加减乘除

    2024年02月12日
    浏览(39)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包