android移动开发实现简单计算器功能,供大家参考,具体内容如下

前言

android 开发小实验
android 移动开发实现 简易计算器功能
小白也能轻松上手,复制粘贴就可使用

使用工具

android studio 或者 intellij idea

首先体验一下结果

预览图

源码

前端页面布局

activity_calculator.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:gravity="center"
              android:orientation="vertical"
              android:background="@drawable/bg2"
>   <textview
        android:layout_width="410dp"
        android:layout_height="60dp"
        android:text="计算器"
        android:textsize="35dp"
        android:gravity="center"
        android:background="#a6efef"
        >
    </textview>
    <textview
            android:id="@+id/text_show"
            android:layout_width="410dp"
            android:layout_height="80dp"
            android:background="#eab9b9"
            android:textsize="30dp"
    />
    <edittext
            android:id="@+id/editext"
            android:layout_width="410dp"
            android:layout_height="70dp"
            android:layout_marginbottom="10dp"
            android:background="#eab9b9"
            android:editable="false"
            android:gravity="right|center_vertical"
            android:paddingright="20dp"
            android:hint="请输入数字"
            android:textsize="30sp"
    />
    <gridlayout
            android:layout_width="410dp"
            android:layout_height="400dp"
            android:columncount="5"
            android:rowcount="6">
        <button
                android:id="@+id/button6"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="←"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/button7"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="ce"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/button8"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="c"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/button9"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="±"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/button10"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="√"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/nine"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="9"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/eight"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="8"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/seven"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="7"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/divider"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="/"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/button15"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="%"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/four"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="4"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/five"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="5"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/six"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="6"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/multiply"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="*"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/button20"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="1/x"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/one"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="1"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/two"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="2"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/three"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="3"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/minus"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="-"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/equal"
                android:layout_width="80dp"
                android:layout_height="100dp"
                android:layout_rowspan="2"
                android:text="="
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/zero"
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:layout_columnspan="2"
                android:text="0"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/point"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="."
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
        <button
                android:id="@+id/add"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="+"
                android:background="#87f4f8"
                android:layout_margin="2dp"
                android:textsize="20dp"
        />
    </gridlayout>
</linearlayout>

后端源码

calculatoractivity.java

package com.example.myappch5;
import android.widget.textview;
import androidx.appcompat.app.appcompatactivity;
import android.os.bundle;
import android.app.activity;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
public class calculatoractivity extends activity implements view.onclicklistener{
    //数字0-9
    button number_0;
    button number_1;
    button number_2;
    button number_3;
    button number_4;
    button number_5;
    button number_6;
    button number_7;
    button number_8;
    button number_9;
    //运算符
    button add;         //+
    button minus;       //-
    button mul;         //*
    button divide;      //除
    button equal;       //=
    button point;       //小数点
    //清除
    button det;
    boolean clean;               //清空标识
    edittext edittext;           //结果显示集
    textview text_show;          //显示计算器文本控件
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_calculator);
        //数字0——9实例化
        number_0=findviewbyid(r.id.zero);
        number_1=findviewbyid(r.id.one);
        number_2=findviewbyid(r.id.two);
        number_3=findviewbyid(r.id.three);
        number_4=findviewbyid(r.id.four);
        number_5=findviewbyid(r.id.five);
        number_6=findviewbyid(r.id.six);
        number_7=findviewbyid(r.id.seven);
        number_8=findviewbyid(r.id.eight);
        number_9=findviewbyid(r.id.nine);
        //运算符实例化
        add=findviewbyid(r.id.add);        //加
        minus=findviewbyid(r.id.minus);         //减
        mul=findviewbyid(r.id.multiply);        //乘
        divide=findviewbyid(r.id.divider);      //除
        equal=findviewbyid(r.id.equal);         //等
        point=findviewbyid(r.id.point);         //小数点
        det=findviewbyid(r.id.button8);         //清除
        //结果显示集
        text_show = findviewbyid(r.id.text_show);
        edittext=findviewbyid(r.id.editext);
//添加事件点击事件
        // 数字点击事件
        number_0.setonclicklistener( this);
        number_1.setonclicklistener(this);
        number_2.setonclicklistener( this);
        number_3.setonclicklistener( this);
        number_4.setonclicklistener(this);
        number_5.setonclicklistener( this);
        number_6.setonclicklistener( this);
        number_7.setonclicklistener(this);
        number_8.setonclicklistener( this);
        number_9.setonclicklistener( this);
        // 符号点击事件
        add.setonclicklistener( this);
        minus.setonclicklistener( this);
        mul.setonclicklistener( this);
        divide.setonclicklistener( this);
        point.setonclicklistener( this);
        equal.setonclicklistener( this);
        det.setonclicklistener( this);
    }
    //读取每个按钮内容
    public void onclick(view view){
        //获取文本内容
        string  input=edittext.gettext().tostring();
        switch (view.getid()){
            case r.id.zero:     // 0
            case r.id.one:      // 1
            case r.id.two:      // 2
            case r.id.three:    // 3
            case r.id.four:     // 4
            case r.id.five:     // 5
            case r.id.six:      // 6
            case r.id.seven:    // 7
            case r.id.eight:    // 8
            case r.id.nine:     // 9
            case r.id.point:    //小数点
                if(clean){
                    clean=false;
                    edittext.settext("");   //赋值为空
                }
                edittext.settext(input+((button)view).gettext()+"");    //结果集就是本身
                break;
            case r.id.add:
            case r.id.minus:          // 减
            case r.id.multiply:       // 乘
            case r.id.divider:        // 除
                if(clean){
                    clean=false;
                    input="";
                    edittext.settext("");
                }
                edittext.settext(input+" "+((button)view).gettext()+" ");
                break;
            case r.id.button8:      //清除
                if(clean){
                    clean=false;
                    input="";
                    edittext.settext("");
                }else  if(input!=null || !input.equals("")){
                    //如果获取内容为空
                    edittext.settext(input.substring(0,input.length() - 1 ));//结果集为空
                    break;
                }
                break;
            case  r.id.equal:   //运算结果=
                getresult();    //调用处理结果方法
                break;
        }
    }
//运算结果方法
    private void getresult(){
        string exp=edittext.gettext().tostring();       //获取文本框内容
        if(exp==null||exp.equals("")){
            return;
        }
        if(!exp.contains("")){
            return;
        }
        if(clean){
            clean=false;
            return;
        }
        clean=true;
        double result=0;
 //进行截取
        // 运算符前的数字
        string s1=exp.substring(0,exp.indexof(" "));
        //运算符
        string op=exp.substring(exp.indexof(" ")+1,exp.indexof(" ")+2);
        //运算符后的数字
        string s2=exp.substring(exp.indexof(" ")+3);
        if(!s1.equals("")&&!s2.equals("")){
            //如果包含小数点的运算
            double d1=double.parsedouble(s1);//则数字都是double类型
            double d2=double.parsedouble(s2);
            if(op.equals("+")){
                // 如果是+
                result=d1+d2;
                text_show.settext(d1+ "+" +d2+"=");
            }else if(op.equals("-")){
                //如果是-
                result=d1-d2;
                text_show.settext(d1+ "-" +d2+"=");
            }else if(op.equals("*")){
                //如果是*
                result=d1*d2;
                text_show.settext(d1+ "*" +d2+"=");
            }else if(op.equals("/")){
                if(d2==0){
                    //如果被除数是0
                    result=0;//则结果为0
                }
                else {
                    //否则执行正常运算
                    result=d1/d2;
                    text_show.settext(d1+ "/" +d2+"=");
                }
            }
            if(!s1.contains(".") &&!s2.contains(".")&&!op.equals("/")){
                //如果是整数类型
                int r=(int)result;//都是整形
                edittext.settext(r+"");
            }else {
                edittext.settext(result+"");
            }
        }else  if(!s1.equals("")&& s2.equals("")){
            //如果只输入运算符前的数字
            edittext.settext(exp);//直接返回当前输入内容
        }else if (s1.equals("")&& !s2.equals("")){
            //如果是只输入运算符后面的数
            double d2 =double.parsedouble(s2);
            //运算符当前没有输入数字
            if(op.equals("+")){
                result= 0 + d2;
                text_show.settext(d2+"=");
            }else  if(op.equals("-")){
                result= 0 - d2;
                text_show.settext(d2+"=");
            }else if (op.equals("*")){
                result= 0;
            }else  if(op.equals("/")){
                result= 0;
            }
            if(!s1.contains(".")&&!s2.contains(".")){
                int r=(int) result;
                edittext.settext(r+"");
            }else {
                edittext.settext(result+"");
            }
        }else {
            edittext.settext("");
        }
    }
}

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