用android如何实现计算机计算功能

news2024/9/8 23:19:51

一.新建一个项目

步骤:

1.新建项目

2.选择 

 

二.用户界面构建 

找到项目的res的下面layout里面的activity.xml文件进行约束布局界面构建。

activity.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">

        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:hint="输入框" />

        <EditText
            android:id="@+id/ed_output"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:hint="输出口" />
    </GridLayout>

    <GridLayout
        android:layout_width="424dp"
        android:layout_height="329dp"
        android:columnCount="4"
        android:rowCount="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/gridLayout"
        tools:ignore="MissingConstraints">

        <Button
            android:id="@+id/buttonc"
            android:layout_width="180dp"
            android:layout_height="60dp"
            android:layout_columnSpan="2"
            android:backgroundTint="#a6a6a6"
            android:text="c" />

        <Button
            android:id="@+id/buttondel"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:layout_columnSpan="1"
            android:backgroundTint="#a6a6a6"
            android:text="DEL" />

        <Button
            android:id="@+id/buttonchu"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#ff9500"
            android:text="/" />

        <Button
            android:id="@+id/button7"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="7" />

        <Button
            android:id="@+id/button8"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="8" />

        <Button
            android:id="@+id/button9"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="9" />

        <Button
            android:id="@+id/buttoncheng"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#ff9500"
            android:text="*" />

        <Button
            android:id="@+id/button4"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="4" />

        <Button
            android:id="@+id/button5"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="5" />

        <Button
            android:id="@+id/button6"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="6" />

        <Button
            android:id="@+id/buttonjian"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#ff9500"
            android:text="-" />

        <Button
            android:id="@+id/button1"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="3" />

        <Button
            android:id="@+id/buttonjia"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#ff9500"
            android:text="+" />

        <Button
            android:id="@+id/buttonyuliu"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="预留" />

        <Button
            android:id="@+id/button0"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="0" />

        <Button
            android:id="@+id/buttondian"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#333333"
            android:text="." />

        <Button
            android:id="@+id/buttondeng"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:backgroundTint="#ff9500"
            android:text="=" />


    </GridLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

三.设置实现计算功能的关键 

找到Java里面的MainActiviy.java写入实现代码。

MainActiviy.java代码如下:

package com.example.myapplication2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button mbutton1,mbutton2,mbutton3,mbutton4,mbutton5,mbutton6,mbutton7,mbutton8,mbutton9,mbutton0,
            mbuttonc,mbuttondel,mbuttonyuliu,mbuttonjia,mbuttonjian,
            mbuttoncheng,mbuttonchu,mbuttondian,mbuttondeng;
    private EditText edinput,edoutput;
    private boolean deng_flag=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //数字0-9
        mbutton1=findViewById(R.id.button1);
        mbutton2=findViewById(R.id.button2);
        mbutton3=findViewById(R.id.button3);
        mbutton4=findViewById(R.id.button4);
        mbutton5=findViewById(R.id.button5);
        mbutton6=findViewById(R.id.button6);
        mbutton7=findViewById(R.id.button7);
        mbutton8=findViewById(R.id.button8);
        mbutton9=findViewById(R.id.button9);
        mbutton0=findViewById(R.id.button0);
        //c、del、预留
        mbuttonc=findViewById(R.id.buttonc);
        mbuttondel=findViewById(R.id.buttondel);
        mbuttonyuliu=findViewById(R.id.buttonyuliu);
        //加减乘除、点、等号
        mbuttonjia=findViewById(R.id.buttonjia);
        mbuttonjian=findViewById(R.id.buttonjian);
        mbuttoncheng=findViewById(R.id.buttoncheng);
        mbuttonchu=findViewById(R.id.buttonchu);
        mbuttondeng=findViewById(R.id.buttondeng);
        mbuttondian=findViewById(R.id.buttondian);
        //输入输出
        edinput=findViewById(R.id.ed_input);
        edoutput=findViewById(R.id.ed_output);

        //设置按钮监听
        //0-9
        mbutton0.setOnClickListener(this);
        mbutton1.setOnClickListener(this);
        mbutton2.setOnClickListener(this);
        mbutton3.setOnClickListener(this);
        mbutton4.setOnClickListener(this);
        mbutton5.setOnClickListener(this);
        mbutton6.setOnClickListener(this);
        mbutton7.setOnClickListener(this);
        mbutton8.setOnClickListener(this);
        mbutton9.setOnClickListener(this);
        //c、del、预留
        mbuttonc.setOnClickListener(this);
        mbuttondel.setOnClickListener(this);
        mbuttonyuliu.setOnClickListener(this);
        //加减乘除、点、等号
        mbuttonjia.setOnClickListener(this);
        mbuttonjian.setOnClickListener(this);
        mbuttoncheng.setOnClickListener(this);
        mbuttonchu.setOnClickListener(this);
        mbuttondeng.setOnClickListener(this);
        mbuttondian.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        String input = edinput.getText().toString();
        String output = edoutput.getText().toString();
        switch (view.getId()){
            //0-9
            case R.id.button0:
            case R.id.button1:
            case R.id.button2:
            case R.id.button3:
            case R.id.button4:
            case R.id.button5:
            case R.id.button6:
            case R.id.button7:
            case R.id.button8:
            case R.id.button9:
            case R.id.buttondian:
                if(deng_flag){
                    deng_flag=false;
                    edinput.setText(null);
                    edinput.setText(((Button) view).getText());
                }else {
                    edinput.setText(input+((Button) view).getText());
                }
                edinput.setText(input+((Button) view).getText());
                break;
            //c
            case R.id.buttonc:
                edinput.setText(null);
                edoutput.setText(null);
                break;
            //del
            case R.id.buttondel:
                if (deng_flag){
                    deng_flag=false;
                    edinput.setText("");
                }else if(input !=null&&!input.equals("")){
                    edinput.setText(input.substring(0,input.length()-1));
                }
                break;
            //预留
            case R.id.buttonyuliu:
                break;
            //加减乘除
            case R.id.buttonjia:
            case R.id.buttonjian:
            case R.id.buttoncheng:
            case R.id.buttonchu:
                edinput.setText(input+" "+((Button) view).getText()+" ");
                break;
            //等号
            case R.id.buttondeng:
//                edinput.setText(input+((Button) view).getText());
//                break;
                getResult();

        }

    }

    private void getResult() {
        try{
            String input = edinput.getText().toString();
            int iResult=0;
            double dResult=0;
            String cw="错误";
            String s1,s2,op;//数字,数字,操作符 s1"4" op"*" s2"5"
            s1=input.substring(0,input.indexOf(" "));
            op=input.substring(input.indexOf(" ")+1,input.indexOf(" ")+2);
            s2=input.substring(input.indexOf(" ")+3);

            double d1,d2;
            d1=Double.parseDouble(s1);
            d2=Double.parseDouble(s2);

            if(op.equals("+")){//加
                dResult=d1+d2;
//                edoutput.setText(dResult+"");
            }else if(op.equals("-")){//减
                dResult=d1-d2;
            } else if (op.equals("*")){//乘
                dResult=d1*d2;
            } else if (op.equals("/")) {//除
                if(d2==0){
                    edoutput.setText(cw+"");
                } else if (d1==0) {
                    dResult=0;
                } else {
                    dResult=d1/d2;
                }
            }
            if(!input.equals(".")&&!input.equals("/")){
                iResult=(int)dResult;
                edoutput.setText(iResult+"");
            }
            edoutput.setText(dResult+"");

        }catch (Exception e){
            System.out.println(e);
        }
    }
}

运行结果如下:

输入计算值,得出结果

 

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Word页码设置,封面无页码,目录摘要阿拉伯数字I,II,III页码,正文开始123为页码

一、背景 使用Word写项目书或论文时&#xff0c;需要正确插入页码&#xff0c;比如封面无页码&#xff0c;目录摘要阿拉伯数字I&#xff0c;II&#xff0c;III为页码&#xff0c;正文开始以123为页码&#xff0c;下面介绍具体实施方法。 所用Word版本&#xff1a;2021 二、W…

【机器学习】第3章 K-近邻算法

一、概念 1.K-近邻算法&#xff1a;也叫KNN 分类 算法&#xff0c;其中的N是 邻近邻居NearestNeighbor的首字母。 &#xff08;1&#xff09;其中K是特征值&#xff0c;就是选择离某个预测的值&#xff08;例如预测的是苹果&#xff0c;就找个苹果&#xff09;最近的几个值&am…

Ollama(docker)+ Open Webui(docker)+Comfyui

Windows 系统可以安装docker desktop 相对比较好用一点&#xff0c;其他的应该也可以 比如rancher desktop podman desktop 安装需要windows WSL 安装ollama docker docker run -d --gpusall -v D:\ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama 这里…

AI视频智能监管赋能城市管理:打造安全有序的城市环境

一、方案背景 随着城市化进程的加速和科技的飞速发展&#xff0c;街道治安问题日益凸显&#xff0c;治安监控成为维护社会稳定和保障人民安全的重要手段。当前&#xff0c;许多城市已经建立了较为完善的治安监控体系&#xff0c;但仍存在一些问题。例如&#xff0c;监控设备分…

20240619在飞凌OK3588-C的Linux R4系统下查找MIPI YUV摄像头的csi size err

20240619在飞凌OK3588-C的Linux R4系统下查找MIPI YUV摄像头的csi size err 2024/6/19 14:00 缘起&#xff0c;公司使用LVDS OUT的机芯&#xff0c;4LANE的LVDS输出。1920x108030分辨率&#xff08;1080p/30&#xff09; 通过FPGA转换为2LANE的MIPI OUT之后进RK3588/OK3588-C。…

加油团油卡密优惠系统开发之多平台兼容性及适配(二)

一、引言 随着科技的快速发展和设备的多样化&#xff0c;确保软件系统在不同平台上的兼容性及适配变得越来越重要。加油团油卡密优惠系统作为一款面向广大用户的在线服务系统&#xff0c;其多平台兼容性及适配的优劣直接影响到用户的体验和使用效果。本文将进一步探讨如何提升…

红队实战宝典之内网渗透测试

本文源自《红队实战宝典之内网渗透测试》一书前言。 近年来&#xff0c;随着计算机网络技术的发展和应用范围的扩大&#xff0c;不同结构、不同规模的局域网和广域网迅速遍及全球。 以互联网为代表的计算机网络技术在短短几十年内经历了从0到1、从简单到复杂的飞速发展&#…

重磅!首个跨平台的通用Linux端间互联组件Klink在openKylin开源

随着智能终端设备的普及&#xff0c;多个智能终端设备之间的互联互通应用场景日益丰富&#xff0c;多设备互联互通应用场景需要开发者单独实现通讯协议。因此&#xff0c;为解决跨平台互联互通问题&#xff0c;由openKylin社区理事单位麒麟软件旗下星光麒麟团队成立的Connectiv…

Python微磁学磁倾斜和西塔规则算法

&#x1f4dc;有限差分-用例 &#x1f4dc;离散化偏微分方程求解器和模型定型 | &#x1f4dc;三维热传递偏微分方程解 | &#x1f4dc;特定资产期权价值偏微分方程计算 | &#x1f4dc;三维波偏微分方程空间导数计算 | &#x1f4dc;应力-速度公式一阶声波方程模拟二维地震波…

tkinter实现一个GUI界面-快速入手

目录 一个简单界面输出效果其他功能插入进度条文本框内容输入和删除标签内容显示和删除 一个简单界面 含插入文本、文本框、按钮、按钮调用函数 # -*- coding: UTF-8 -*-import tkinter as tk from tkinter import END from tkinter import filedialog from tkinter impor…

3d模型有个虚拟外框怎么去除?---模大狮模型网

在3D建模和渲染过程中&#xff0c;虚拟外框(Bounding Box)是一个常见的显示元素&#xff0c;用于表示模型的包围盒或选择状态。尽管虚拟外框在一些情况下有其作用&#xff0c;但在最终渲染或呈现阶段&#xff0c;我们通常希望清除这些辅助显示&#xff0c;以展示纯粹的模型效果…

[图解]企业应用架构模式2024新译本讲解14-服务层2

1 00:00:01,070 --> 00:00:01,820 我们来看案例 2 00:00:02,600 --> 00:00:06,620 案例也同样是之前跟事务脚本 3 00:00:07,030 --> 00:00:09,400 领域模型等等用过的案例是一样的 4 00:00:10,480 --> 00:00:12,700 这里译文改了一些 5 00:00:16,200 --> 00…

ai创作是什么?分享ai创作的方法

ai创作是什么&#xff1f;在当今这个信息爆炸的时代&#xff0c;文字的力量愈发显得重要。无论是日常沟通还是专业创作&#xff0c;我们都需要用文字来表达自己&#xff0c;传递思想。然而&#xff0c;面对海量的信息和快速变化的世界&#xff0c;如何高效地生成高质量的文字内…

高效、智能、稳定,LoRa监测终端为光伏跟踪支架系统保驾护航

在光伏发电领域&#xff0c;光伏跟踪支架作为提高光伏系统发电效率的关键技术之一&#xff0c;已经得到了广泛的应用。然而&#xff0c;如何有效地监测光伏跟踪支架的状态&#xff0c;确保其稳定、高效地运行&#xff0c;一直是业界关注的焦点。近年来&#xff0c;随着物联网技…

基础模型服务商SiliconCloud,新注册用户赠送 14 元的配额(约 1 亿 token)

注册链接&#xff1a;https://cloud.siliconflow.cn?referrerclx1f2kue00005c599dx5u8dz 开源模型可以自己部署&#xff0c;对服务器的要求还是挺高&#xff0c;以及学习成本、部署过程成本都是比较高&#xff0c;硅基流动SiliconFlow提供了另一个方式&#xff0c;可以像使用…

C++ (week8):数据库

文章目录 一、数据库简介1.数据库2.MySQL(1)数据库的结构(2)MySQL的三种使用方式(3)命令行(4)Navicat Premium 二、SQL1.SQL (Structured Query Language)&#xff0c;即结构化查询语言2.数据定义语言 DDL (Data Definition Language) &#xff0c;创建、修改、删除数据库、表结…

LCB模型引领机器人进入端到端新维度

论文标题&#xff1a; From LLMs to Actions: Latent Codes as Bridges in Hierarchical Robot Control 论文作者&#xff1a; Yide Shentu&#xff0c;Philipp Wu&#xff0c;Aravind Rajeswaran&#xff0c;Pieter Abbeel 项目地址&#xff1a; https://fredshentu.gith…

WDF驱动开发-计时器

WDF可以使用框架的内置计时器支持。 它适用于 Kernel-Mode Driver Framework (KMDF) 驱动程序&#xff0c;以及从版本 2 开始的 User-Mode Driver Framework (UMDF) 驱动程序。 框架提供了一个 计时器对象 &#xff0c;使驱动程序能够创建计时器。 在驱动程序创建计时器对象并…

北斗高精度定位终端的应用领域和作用

北斗高精度定位终端的技术进步不仅体现在硬件设备的优化上&#xff0c;还深入到软件算法的革新。采用先进的实时动态差分技术&#xff08;RTK&#xff09;&#xff0c;结合地面增强基站网络&#xff0c;能够大幅度提高定位解算的精度和速度&#xff0c;即使在复杂的城市峡谷或茂…

哪些好用的AI绘画生成软件?建议你试试这四款

哪些好用的AI绘画生成软件&#xff1f;随着人工智能技术的飞速发展&#xff0c;AI绘画生成软件逐渐走入大众的视野&#xff0c;为艺术创作领域带来了革命性的变革。今天&#xff0c;就让我们一起探索四款备受推崇的AI绘画生成软件&#xff0c;看看它们如何以独特的魅力&#xf…