Android实现简单计算器

本文实例为大家分享了Android实现简单计算器的具体代码,供大家参考,具体内容如下

功能

1、加减乘除四则运算
2、归0
3、回退
4、即时运算

配置

在build.gradle(app) 中加入下面的代码

buildFeatures {
        viewBinding = true
    }

加入位置如下所示

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
buildFeatures {
    viewBinding = true
}

布局

<?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:orientation="vertical"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:layout_margin="5dp"
        android:text="计算器\nby csdn weixin_44423317 "
        android:textSize="20sp"
        android:gravity="right|center"
        android:padding="6dp"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:background="#ADADAD"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <Button
                android:id="@+id/ac"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:text="AC"
                android:background="@color/white"/>
            <Button
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="#00FFFFFF"/>
            <Button
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="#00FFFFFF"/>
            <ImageButton
                android:id="@+id/dec"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:layout_gravity="right"
                android:background="@color/white"
                android:src="@drawable/ic_baseline_backspace_24"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <Button
                android:id="@+id/qi"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:text="7"
                android:background="@color/white"/>
            <Button
                android:id="@+id/ba"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:text="8"
                android:background="@color/white"/>
            <Button
                android:id="@+id/jiu"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:text="9"
                android:textSize="20sp"
                android:background="@color/white"/>
            <Button
                android:id="@+id/mul"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:text="x"
                android:background="@color/white"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <Button
                android:id="@+id/si"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textSize="20sp"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:text="4"
                android:background="@color/white"/>
            <Button
                android:id="@+id/wu"
                android:textSize="20sp"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:text="5"
                android:background="@color/white"/>
            <Button
                android:id="@+id/liu"
                android:textSize="20sp"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:text="6"
                android:background="@color/white"/>
            <Button
                android:id="@+id/div"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textSize="20sp"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:text="/"
                android:background="@color/white"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <Button
                android:id="@+id/yi"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textSize="20sp"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:text="1"
                android:background="@color/white"/>
            <Button
                android:id="@+id/er"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:text="2"
                android:background="@color/white"/>
            <Button
                android:id="@+id/san"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:text="3"
                android:background="@color/white"/>
            <Button
                android:id="@+id/sub"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:text="-"
                android:background="@color/white"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <Button
                android:id="@+id/zero"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:text="0"
                android:background="@color/white"/>
            <Button
                android:id="@+id/point"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:text="."
                android:background="@color/white"/>
            <Button
                android:id="@+id/equal"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:text="="
                android:background="@color/white"/>
            <Button
                android:id="@+id/add"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:text="+"
                android:background="@color/white"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String tv="0";
    private ActivityMainBinding inflate;
    private String count="";
    private boolean isMax=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflate = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(inflate.getRoot());
        inflate.zero.setOnClickListener(this);
        inflate.yi.setOnClickListener(this);
        inflate.er.setOnClickListener(this);
        inflate.san.setOnClickListener(this);
        inflate.si.setOnClickListener(this);
        inflate.wu.setOnClickListener(this);
        inflate.liu.setOnClickListener(this);
        inflate.qi.setOnClickListener(this);
        inflate.ba.setOnClickListener(this);
        inflate.jiu.setOnClickListener(this);
        inflate.add.setOnClickListener(this);
        inflate.sub.setOnClickListener(this);
        inflate.mul.setOnClickListener(this);
        inflate.div.setOnClickListener(this);
        inflate.point.setOnClickListener(this);
        inflate.equal.setOnClickListener(this);
        inflate.dec.setOnClickListener(this);
        inflate.ac.setOnClickListener(this);
    }
    private void jisuanqi(){
        List<String> strs=new ArrayList<>();
        StringBuilder f= new StringBuilder();
        for(int i=0;i<tv.length();i++) {
            if(String.valueOf(tv.charAt(i)).equals("-")) {
                if(!f.toString().equals("")){
                    strs.add(f.toString());
                    strs.add("-");
                    f = new StringBuilder();
                }else{
                    f.append("-");
                }
                continue;
            }
            if(String.valueOf(tv.charAt(i)).matches("(\\d+)|(\\.)")) {
                f.append(String.valueOf(tv.charAt(i)));
            }else {
                strs.add(f.toString());
                strs.add(String.valueOf(tv.charAt(i)));
                f = new StringBuilder();
            }
        }
        if(!f.toString().equals("")) {
            strs.add(f.toString());
        }
        if(!strs.get(strs.size()-1).matches("(\\-|)?\\d+(\\.\\d*)?$")){
            strs.remove(strs.size()-1);
        }
        double ans=number(strs,0);
        if(ans==0||ans%(int)ans==0){
            count=(int)ans+"";
        }else {
            BigDecimal bigdecimal=new BigDecimal(ans);
            String a=bigdecimal.setScale(12,5).toString();
            if(ans-(int)ans!=0) {
                while (String.valueOf(a.charAt(a.length() - 1)).equals("0")){
                    a=a.substring(0,a.length()-1);
                }
            }
            count=a;
        }
        if(isMax){
            isMax=false;
            count="∞";
        }
        showText();
    }
    private void showText(){
        inflate.tv.setText(tv);
        SpannableString spanText  =  new SpannableString(count);
        spanText.setSpan(new AbsoluteSizeSpan(30, true), 0, spanText.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        inflate.tv.append("\n");
        inflate.tv.append(spanText);
    }
    private double number(List<String> n,int v) {
        double con = 0;
        int j=0;
        con=Double.valueOf(n.get(0));
        if(v==0) {
            for(int i=0;i<n.size();i++) {
                if(n.get(i).matches("(\\-|)?\\d+(\\.\\d+)?$")) {//55+5*5/5-5=
                    con=Double.valueOf(n.get(i));
                    if(n.size()>i+1) {
                        j=i+1;
                    }
                }
                if(n.get(j).equals("x")){
                    n.set(i, String.valueOf(con*Double.valueOf(n.get(i+2))));
                    n.remove(i+1);
                    n.remove(i+1);
                    return number(n,0);
                }else if(n.get(j).equals("/")){
                    if(Double.valueOf(n.get(i+2))==0){
                        isMax=true;
                        return 10;
                    }
                    n.set(i, String.valueOf(con/Double.valueOf(n.get(i+2))));
                    n.remove(i+1);
                    n.remove(i+1);
                    return number(n,0);
                }
            }
        }
        for(int i=0;i<n.size();i++) {
            if(n.get(i).matches("(\\-|)?\\d+(\\.\\d+)?$")) {//55+5*5/5-5=
                con=Double.valueOf(n.get(i));
                if(n.size()>i+1) {
                    j=i+1;
                }
            }
            if(n.get(j).equals("+")){
                n.set(i, String.valueOf(con+Double.valueOf(n.get(i+2))));
                n.remove(i+1);
                n.remove(i+1);
                return number(n,1);

            }else if(n.get(j).equals("-")){
                n.set(i, String.valueOf(con-Double.valueOf(n.get(i+2))));
                n.remove(i+1);
                n.remove(i+1);
                return number(n,1);
            }

        }
        return con;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.point:
                if(String.valueOf(tv.charAt(tv.length()-1)).matches("\\d+")){
                    tv+=".";
                    inflate.tv.setText(tv);
                }
                break;
            case R.id.zero:
            case R.id.yi:
            case R.id.er:
            case R.id.san:
            case R.id.si:
            case R.id.wu:
            case R.id.liu:
            case R.id.qi:
            case R.id.ba:
            case R.id.jiu:
                if(tv.equals("0")){
                    tv="";
                }
                Button bt_digit=findViewById(v.getId());
                tv+=bt_digit.getText();
                inflate.tv.setText(tv);
                count=tv;
                jisuanqi();
                break;
            case R.id.sub:
                if(String.valueOf(tv.charAt(0)).equals("0")){
                    tv="-";
                }else {
                    if(String.valueOf(tv.charAt(tv.length()-1)).equals(".")){
                        tv += "0";
                    }
                    tv+="-";
                }
                showText();
                break;
            case R.id.add:
            case R.id.mul:
            case R.id.div:
                Button bt_operator=findViewById(v.getId());
                String laststr=String.valueOf(tv.charAt(tv.length()-1));
                if(!laststr.matches("(\\d+)") ) {
                    tv = tv.substring(0, tv.length() - 1) + bt_operator.getText();
                }else {
                    if(laststr.equals(".")) {
                        tv += "0";
                    }
                    tv+=bt_operator.getText();
                }

                showText();
                break;
            case R.id.ac:
                tv="0";
                inflate.tv.setText(tv);
                break;
            case R.id.dec:
                if(tv.length()>1) {
                    tv = tv.substring(0, tv.length() - 1);
                }else {
                    tv="0";
                }
                jisuanqi();
                break;
            case R.id.equal:
                if(!String.valueOf(tv.charAt(tv.length()-1)).matches("(\\d+)")){
                    tv = tv.substring(0, tv.length() - 1);
                }
                count=tv;
                jisuanqi();
                tv=count;
                inflate.tv.setText(tv);
                break;
        }
    }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现简易计算器小程序

    本文实例为大家分享了Android实现简易计算器小程序的具体代码,供大家参考,具体内容如下 目标效果: 通过编写代码,可以实现整数和小数的加减乘除运算,以及删除和清空的功能. 1.页面中Button使用的是线性布局,最外边一个是父布局,第一行C,DEL,/,*为第一个子布局,第二行7,8,9,-为第二个子布局,第三行4,5,6,+为第三个子布局,第四五行为第四个子布局,第四个子布局中还有两个相当于是孙布局的级别,1,2,3为第一个孙布局,0和.为第二个孙布局,=在两个孙布局之外第四个子布局以内.

  • Android Studio实现简易计算器

    如果是制作简易计算器的话是基本没有难点的,供大家参考,具体内容如下 步骤是先写好界面布局,将按钮的布局.字号颜色啥的做好,再就是设置监听器. 使用了NoTitleBar的主题 代码如下: activity_main里关于界面的代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

  • 从零开始学android实现计算器功能示例分享(计算器源码)

    下面是效果展示: 复制代码 代码如下: <?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

  • android计时器,时间计算器的实现方法

    需求:默认为"00:00:00",点击开始按钮时清零后开始计时,出现如10:28:34.点击停止的时候停止计时.问题:使用Calendar DateFormat的方法,不设置时区获取到的小时是本地时区的(东八区的就是8),设置成GMT标准时区获取到的时间是12小时(12:00:00),设置24小时制无效.在开始时间加减各种小时都无效,而且计时只能到12小时就自动跳上去了,始终无法出现默认状态00:00:00开始计时的效果.尝试各种时间设置方法无效后只能自己写一个根据秒数转换时间格式字符

  • Android studio设计简易计算器

    本文实例为大家分享了Android studio设计简易计算器的具体代码,供大家参考,具体内容如下 效果显示: 第一步,简单的界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and

  • android计算器简单实现代码

    本文实例为大家分享了android计算器的具体实现代码,供大家参考,具体内容如下 java代码: package com.itheima74.simplecalculator4; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; imp

  • android计算器代码示例分享

    复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><EditText    android:id="@+id/editText1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:ems="10&quo

  • Android计算器编写代码

    其实这个安卓计算机,所有的后台思想与<C#计算器编写代码>是一模一样的.Win窗体程序移植到安卓,从C#到Java其实很简单的,因为两者的基本语法都很相像,唯一的难点是安卓的xml布局部分,不像C#窗体能够直接拖.  还是如下图一个能够完成基本四则运算的计算器: 先在res\values\strings.xml设置按钮相应的字体,以免布局文件警告满天飞: <?xml version="1.0" encoding="utf-8"?> <r

  • android studio实现计算器

    本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下 效果图: 资源文件: color.xml <?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark&

  • android实现简单计算器功能

    本文实例为大家分享了android实现简单计算器的具体代码,供大家参考,具体内容如下 // 日期:2014/9/26 // 首先,人们的输入习惯为中缀表达式.为了便于计算,程序会将中缀表达式会转换为后缀表达式 //////////////////////////////////////////////////////////////////////////////////////// // 目前软件还存在 // 1.输入运算数和运算符不匹配时,崩溃的现象.(如:只输入一个操作数) // 2.一个

  • Android开发简单计算器实现代码

    计算器项目,要求实现加.减.乘.除.求倒数.求平方根等简单运算. 真机调试结果如下图: 布局文件:main_activity.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pare

  • Android实现简单计算器

    本文实例为大家分享了Android实现简单计算器的具体代码,供大家参考,具体内容如下 功能 1.加减乘除四则运算 2.归0 3.回退 4.即时运算 配置 在build.gradle(app) 中加入下面的代码 buildFeatures { viewBinding = true } 加入位置如下所示 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSIO

  • Android实现简单计算器界面

    本文实例为大家分享了Android实现计算器界面的具体代码,供大家参考,具体内容如下 XML文件: <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_

  • Android开发实现简单计算器功能

    计算器项目,要求实现加.减.乘.除.求倒数.求平方根等简单运算. 真机调试结果如下图: 布局文件:main_activity.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pare

  • Android开发实现的简单计算器功能【附完整demo源码下载】

    本文实例讲述了Android开发实现的简单计算器功能.分享给大家供大家参考,具体如下: 这个Android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦: 先上图看效果 比较简单,所以我就没怎么写注释,应该一看就能明白的 有不明白的可以发信问我 先贴MainActivity.java代码 package com.example.calculator; import android.app.Activity; import android.os.Bundle; import andro

  • Android studio实现简单计算器

    本文实例为大家分享了Android studio实现简单计算器的具体代码,供大家参考,具体内容如下 需求分析 在Android studio中设计并实现一个简单的计算器,实现连续的加减乘除运算. 界面设计 采用网格GridLayout布局,设计了一个6行4列的网格,最上边是一个EditText用来显示用户输入的运算数字和运算符,以及相关的运算结果,其占4列,文本框大小为50dip:依次往下的界面分别设置了数字和运算符以及操作的按钮,各行各列的每个按钮的大小均设为26sp. 编程分析 设计了两个文

  • Android实现简单加法计算器

    本文实例为大家分享了Android实现简单加法计算器的具体代码,供大家参考,具体内容如下 package com.example.calculator; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; impo

  • Android Studio实现简单计算器功能

    本文实例为大家分享了Android Studio实现简单计算器功能的具体代码,供大家参考,具体内容如下 程序步骤: (1)在布局文件定义一些计算器界面的文本框,按钮等组件. (2)在Activity中获取组件实例. (3)通过swtich函数,判断输入的内容,并进行相应操作,通过getText()获取文本内容,setText()显示. 程序代码设计: 一.界面布局 参考书本例2-9,应用网格布局设计计算器界面,在设计区域设置一个6行4列的网格布局,第一行为显示数据的文本标签,第二行为清除数据的按

  • Android table布局开发实现简单计算器

    本文实例为大家分享了Android table布局开发实现简单计算器的具体代码,供大家参考,具体内容如下 结果如图: XML文件如下: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android

随机推荐