&…">
当前位置:首页 > 谈天说地

Android Studio实现简易计算器源码

34资源网2022-05-22448

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

效果图:

源码:

布局样式:

<?xml version="1.0" encoding="utf-8"?>
<gridlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselinealigned="false"
    android:background="@color/grey"
    android:columncount="4"
    android:rowcount="6">
    <textview
        android:id="@+id/textview_01"
        android:layout_columnspan="4"
        android:layout_gravity="fill"
        android:gravity="right"
        android:layout_marginleft="5dp"
        android:layout_marginright="5dp"
        android:background="@color/grey"
        android:paddingright="20dp"
        android:paddingtop="40dp"
        android:text="0"
        android:layout_columnweight="1"
        android:textsize="88sp">
    </textview>
    <button
        android:id="@+id/button_return"
        android:layout_columnspan="2"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="2dp"
        android:background="@color/white"
        android:textsize="30dp"
        android:textstyle="bold"
        android:text="回退" />
    <button
        android:id="@+id/button_clear"
        android:layout_columnspan="2"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="2dp"
        android:background="@color/white"
        android:textsize="30dp"
        android:textstyle="bold"
        android:text="清空"/>
    <button
        android:id="@+id/button_add"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="+"/>
    <button
        android:id="@+id/button_01"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="1"/>
    <button
        android:id="@+id/button_02"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="2"/>
    <button
        android:id="@+id/button_03"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="3"/>
    <button
        android:id="@+id/button_reduce"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="-"/>
    <button
        android:id="@+id/button_04"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="4"/>
    <button
        android:id="@+id/button_05"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="5"/>
    <button
        android:id="@+id/button_06"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="6"/>
    <button
        android:id="@+id/button_ride"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="*"/>
    <button
        android:id="@+id/button_07"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="7"/>
    <button
        android:id="@+id/button_08"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="8"/>
    <button
        android:id="@+id/button_09"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="9"/>
    <button
        android:id="@+id/button_except"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="/"/>
    <button
        android:id="@+id/button_spot"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="."/>
    <button
        android:id="@+id/button_00"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="0"/>
    <button
        android:id="@+id/button_equal"
        android:layout_columnspan="1"
        android:gravity="center"
        android:layout_columnweight="1"
        android:layout_rowweight="1"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:textsize="50dp"
        android:text="="/>
</gridlayout>

java部分源码:

package com.example.pyfcalculator;
 
import androidx.appcompat.app.appcompatactivity;
 
import android.os.bundle;
import android.text.textutils;
import android.view.view;
import android.widget.button;
import android.widget.textview;
 
public class mainactivity extends appcompatactivity implements view.onclicklistener {
 
    textview textview;
    button button_return,button_00,button_01,button_02,button_03,
            button_04,button_05,button_06,button_07,button_08,button_09,button_add,
            button_clear,button_equal,button_except,button_reduce,button_ride,button_spot;
 
 
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        initviewandlistener();
    }
 
 
    //注册监听方法
    protected void initviewandlistener(){
        button_return=findviewbyid(r.id.button_return);
        button_00=findviewbyid(r.id.button_00);
        button_01=findviewbyid(r.id.button_01);
        button_02=findviewbyid(r.id.button_02);
        button_03=findviewbyid(r.id.button_03);
        button_04=findviewbyid(r.id.button_04);
        button_05=findviewbyid(r.id.button_05);
        button_06=findviewbyid(r.id.button_06);
        button_07=findviewbyid(r.id.button_07);
        button_08=findviewbyid(r.id.button_08);
        button_09=findviewbyid(r.id.button_09);
        button_add=findviewbyid(r.id.button_add);
        button_clear=findviewbyid(r.id.button_clear);
        button_equal=findviewbyid(r.id.button_equal);
        button_except=findviewbyid(r.id.button_except);
        button_reduce=findviewbyid(r.id.button_reduce);
        button_ride=findviewbyid(r.id.button_ride);
        button_spot=findviewbyid(r.id.button_spot);
 
        button_return.setonclicklistener(this);
        button_00.setonclicklistener(this);
        button_01.setonclicklistener(this);
        button_02.setonclicklistener(this);
        button_03.setonclicklistener(this);
        button_04.setonclicklistener(this);
        button_05.setonclicklistener(this);
        button_06.setonclicklistener(this);
        button_07.setonclicklistener(this);
        button_08.setonclicklistener(this);
        button_09.setonclicklistener(this);
        button_add.setonclicklistener(this);
        button_clear.setonclicklistener(this);
        button_equal.setonclicklistener(this);
        button_except.setonclicklistener(this);
        button_reduce.setonclicklistener(this);
        button_ride.setonclicklistener(this);
        button_spot.setonclicklistener(this);
 
        textview=findviewbyid(r.id.textview_01);
    }
 
    private string num_in_now;//记录当前结果
    private boolean flag=false;//记录计算器状态
    int now_num=0;
 
    @override
    public void onclick(view view) {
 
        //获取文本框内容
        string currenttext=textview.gettext().tostring();
 
        switch (view.getid()){
            case r.id.button_00:
                if ((textview.gettext().tostring().equals("0")))
                    return;
                isflag("0");
                break;
            case r.id.button_01:
                isflag("1");
                break;
            case r.id.button_02:
                isflag("2");
                break;
            case r.id.button_03:
                isflag("3");
                break;
            case r.id.button_04:
                isflag("4");
                break;
            case r.id.button_05:
                isflag("5");
                break;
            case r.id.button_06:
                isflag("6");
                break;
            case r.id.button_07:
                isflag("7");
                break;
            case r.id.button_08:
                isflag("8");
                break;
            case r.id.button_09:
                isflag("9");
                break;
            case r.id.button_add:
//                textview.settext(textview.gettext()+"+");
                now_num++;
                if(now_num>1){
                    equal();
                }
                isflag("+");
                break;
            case r.id.button_except:
//                textview.settext(textview.gettext()+"/");
                now_num++;
                if(now_num>1){
                    equal();
                }
                isflag("/");
                break;
            case r.id.button_spot:
//                textview.settext(textview.gettext()+".");
                isflag(".");
                break;
            case r.id.button_reduce:
//                textview.settext(textview.gettext()+"-");
                now_num++;
                if(now_num>1){
                    equal();
                }
                isflag("-");
                break;
            case r.id.button_ride:
//                textview.settext(textview.gettext()+"*");
                now_num++;
                if(now_num>1){
                    equal();
                }
                isflag("*");
                break;
            case r.id.button_clear:
                textview.settext("0");
                flag=false;
                now_num=0;
                break;
            case r.id.button_return:
                if(textutils.isempty(textview.gettext())){
                    return;
                }
                if (currenttext.substring(0,currenttext.length()-1).length()>0){
                    textview.settext(currenttext.substring(0,currenttext.length()-1));
                }
                else{
                    textview.settext("0");
                    flag=false;
                    now_num=0;
                }
                break;
            case r.id.button_equal:
                if(textutils.isempty(textview.gettext())){
                    return;
                }
                equal();
                flag=false;
                now_num=0;
                break;
        }
 
    }
 
    private void equal(){
        textview.settext(operation(textview.gettext().tostring()));
    }
 
    //运算方法
    private string operation(string string){
        int index;
        double num_1,num_2;
        if (string.contains("+")){
            index=string.indexof("+");
            num_1= double.parsedouble(string.substring(0,index));
            num_2= double.parsedouble(string.substring(index+1,string.length()));
            return num_1+num_2+"";
        }
        if (string.contains("-")){
            index=string.indexof("-");
            if(index==0){
                string string_2=string.substring(1,string.length());
                int index_2=string_2.indexof("-");
                num_1= double.parsedouble(string_2.substring(0,index_2));
                num_2= double.parsedouble(string_2.substring(index_2+1,string_2.length()));
                return (-(num_1+num_2))+"";
            }else{
                num_1= double.parsedouble(string.substring(0,index));
                num_2= double.parsedouble(string.substring(index+1,string.length()));
                return num_1-num_2+"";
            }
 
        }
        if (string.contains("*")){
            index=string.indexof("*");
            num_1= double.parsedouble(string.substring(0,index));
            num_2= double.parsedouble(string.substring(index+1,string.length()));
            return num_1*num_2+"";
        }
        if (string.contains("/")){
            index=string.indexof("/");
            num_1= double.parsedouble(string.substring(0,index));
            num_2= double.parsedouble(string.substring(index+1,string.length()));
            return num_1/num_2+"";
        }
        return "error!!!";
    }
 
    //判断0要不要被替换 看flag的状态 输入数字
    private void isflag(string info){
        if (flag){
            textview.settext(textview.gettext()+info);
        }else{
            if(info.equals(".")||info.equals("+")||info.equals("-")||info.equals("*")||info.equals("/")){
                textview.settext(textview.gettext()+info);
            }else {
                textview.settext(info);
            }
            flag=true;
        }
    }
 
}

其余资源没有太大难度,就不一展现了!

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

看完文章,还可以扫描下面的二维码下载快手极速版领4元红包

快手极速版二维码

快手极速版新人见面礼

除了扫码领红包之外,大家还可以在快手极速版做签到,看视频,做任务,参与抽奖,邀请好友赚钱)。

邀请两个好友奖最高196元,如下图所示:

快手极速版邀请好友奖励

扫描二维码推送至手机访问。

版权声明:本文由34楼发布,如需转载请注明出处。

本文链接:https://www.34l.com/post/15957.html

分享给朋友:

相关文章

最傻的六种员工离职理由,希望你不是其中一员

最傻的六种员工离职理由,希望你不是其中一员

春节后,今年的中国员工离职率应该是最低的。但是职场上,还是会有很多员工提离职。离职原因各种各样, 不开心,不舒服,工资少,学不到东西等等。那么,最傻的六种员工离职是哪些呢?…

闪客快跑2背景音乐(闪客快打andylaw的微博)

闪客快跑2背景音乐(闪客快打andylaw的微博)

《疯狂跑酷》是一款LowPoly(低多边形)画风的跑酷游戏,一场突如其来的大水淹没了城市,而你在游戏中扮演一名刚下班的男子,需要从被水淹没的城市中逃出生天。…

这家带有ESG基因的SaaS公司,如何定义未来的服装行业?

这家带有ESG基因的SaaS公司,如何定义未来的服装行业?

编者按:本文转自GGV纪源资本,创业邦经授权转载。 随着新一轮科技革命和产业变革,服装行业进行数智化转型升级已经成为了行业共识,但是多数企业对服装智能制造转型升级系统性认识不足,传统大批量流水线的生产方式无法满足市场“多款式”、“小批量”、…

共享系统开发多少钱(分享共享模式系统)

共享系统开发多少钱(分享共享模式系统)

现在,越来越多地商家都开始自己定制软件系统,都想争先抢占自己地私域流量,所以大家最想了解地是系统开发地方案和报价都有哪些,那么今天小编就来给大家讲一下关于系统开发方案和报价地一些知识。 一、系统开发方案1、系统定位首先,在一开始,不光要商…

联想G470怎么样换电池(联想G470详细拆卸图文教程)

联想G470怎么样换电池(联想G470详细拆卸图文教程)

一台联想G470客户描述不充电,在系统里面可以认到电池,拆机,G470修到不少了,大部分都是保护隔离第一个管子挂掉或者公共点短路,或者是CPU没供电等问题。那就测量一下保护隔离的几个管子吧,结果管子没有短路,测量充电芯片ISL6251的DC…