文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android计算器简单逻辑实现实例分享

2022-06-06 10:31

关注

引言:

  我的android计算器的实现方式是:按钮输入一次,就处理一次。

  但是如果你学过数据结构(栈),就可以使用表达式解析(前缀,后缀)处理。

  而这个方式已经很成熟了,但是时间有限,只完成了这个简单的计算器。

  至于,这个Android的布局已经在我博客中发布了,不再讲述。    
代码如下:
package com.example.androidlessontwo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity  {

    private Button[] buttonNum=new Button[11];
    private Button[] buttonComand=new Button[5];
    private TextView input=null;
    private TextView rl=null;
    private Button   buttonClear=null;
    private boolean firstFlag=true;
    private double result=0.0;
    private String lastCommand;
    public void MyCalculator()
    {
        result = 0.0;
        firstFlag=true;
        lastCommand="=";
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonNum[0]=(Button) findViewById(R.id.num0);
        buttonNum[1]=(Button) findViewById(R.id.num1);
        buttonNum[2]=(Button) findViewById(R.id.num2);
        buttonNum[3]=(Button) findViewById(R.id.num3);
        buttonNum[4]=(Button) findViewById(R.id.num4);
        buttonNum[5]=(Button) findViewById(R.id.num5);
        buttonNum[6]=(Button) findViewById(R.id.num6);
        buttonNum[7]=(Button) findViewById(R.id.num7);
        buttonNum[8]=(Button) findViewById(R.id.num8);
        buttonNum[9]=(Button) findViewById(R.id.num9);
        buttonNum[10]=(Button) findViewById(R.id.point);
        buttonComand[0]=(Button) findViewById(R.id.add);
        buttonComand[1]=(Button) findViewById(R.id.sub);
        buttonComand[2]=(Button) findViewById(R.id.ride);
        buttonComand[3]=(Button) findViewById(R.id.divide);
        buttonComand[4]=(Button) findViewById(R.id.equal);
        input=(TextView) findViewById(R.id.input);
        rl   =(TextView) findViewById(R.id.rl);
        buttonClear=(Button) findViewById(R.id.clean);
        NumberAction na= new NumberAction();
        CommandAction ca=new CommandAction();
        for(Button bc:buttonComand)
        {
            bc.setOnClickListener(ca);
        }
        for(Button bc:buttonNum)
        {
            bc.setOnClickListener(na);
        }
        buttonClear.setOnClickListener(new Button.OnClickListener()
        {

            @Override
            public void onClick(View v) {
                MyCalculator();
                rl.setText("0.0");
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private class NumberAction implements Button.OnClickListener
    {

        @Override
        public void onClick(View view)
        {
            Button btn = (Button)view;
            String inputTemp =btn.getText().toString();//6
            input.setText(input.getText().toString()+inputTemp);   
            double numtemp = 0;
            switch(btn.getId())
            {
                case R.id.num0:
                {
                    if(firstFlag)
                        {
                            result=result*10+0;
                            firstFlag=false;
                        }
                    else
                        numtemp=numtemp*10+0;
                    break;
                }
                case R.id.num1:
                {
                    if(firstFlag)
                        {
                        result=result*10+1;
                            firstFlag=false;
                        }
                    else
                        numtemp=numtemp*10+1;
                    break;
                }
                case R.id.num2:
                {
                    if(firstFlag)
                        {
                        result=result*10+2;
                            firstFlag=false;
                        }
                    else
                        numtemp=numtemp*10+2;
                    break;
                }
                case R.id.num3:
                {
                    if(firstFlag)
                        {
                        result=result*10+3;
                            firstFlag=false;
                        }
                    else
                        numtemp=numtemp*10+3;
                    break;
                }
                case R.id.num4:
                {
                    if(firstFlag)
                        {
                        result=result*10+4;
                            firstFlag=false;
                        }
                    else
                        numtemp=numtemp*10+4;
                    break;
            &nbs p;   }
                case R.id.num5:
                {
                    if(firstFlag)
                        {
                        result=result*10+5;
                            firstFlag=false;
                        }
                    else
                        numtemp=numtemp*10+5;
                    break;
                }
                case R.id.num6:
                {
                    if(firstFlag)
                        {
                        result=result*10+6;
                            firstFlag=false;
                        }
                    else
                        {
                            numtemp=numtemp*10+6;
                            calculate(numtemp);
                        }
                    break;
                }
                case R.id.num7:
                {
                    if(firstFlag)
                        {
                             result=result*10+7;
                            firstFlag=false;
                        }
                    else
                    {
                        numtemp=numtemp*10+7;
                        calculate(numtemp);
                    }
                    break;
                }
                case R.id.num8:
                {
                    if(firstFlag)
                        {
                        result=result*10+8;
                            {
                                result=result*10+8;
                                firstFlag=false;
                            }
                        }
                    else
                        {
                            numtemp=numtemp*10+8;
                            calculate(numtemp);
                        }
                    break;
                }
                case R.id.num9:
                {
         &nbs p;          if(firstFlag)
                        {
                        result=result*10+9;
                            firstFlag=false;
                        }
                    else
                        {
                            numtemp=numtemp*10+9;
                            calculate(numtemp);
                        }
                    break;
                }   
            }           
           
           
        }
    }
    private class CommandAction implements Button.OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            Button btn=(Button)v;
            String inputCommand=(String)btn.getText();
            switch(btn.getId())
            {
                case R.id.add:
                {
                    lastCommand="+";
                    break;
                }
                case R.id.sub:
                {
                    lastCommand="-";
                    break;
                }
                case R.id.ride:
                {
      ;               lastCommand="*";
                    break;
                }
                case R.id.divide:
                {
                    lastCommand="/";
                    break;
                }
                case R.id.equal:
                {
                    lastCommand="=";
                    input.setText("");
                    rl.setText(String.valueOf(result));
                    return ;
                }
            }
            input.setText(input.getText()+inputCommand);   
        }
    }
    private void calculate(double x)
    {
       
         if(lastCommand.equals("+"))
            {
                result += x;
            }
         if(lastCommand.equals("-"))
            {
                result -= x;
            }
         if(lastCommand.equals("*"))
            {
                result *= x;
            }
         if(lastCommand.equals("/"))
            {
                result /= x;
            }
    }
}

您可能感兴趣的文章:Android Studio实现简易计算器从零开始学android实现计算器功能示例分享(计算器源码)android计时器,时间计算器的实现方法Android计算器编写代码android计算器简单实现代码Android开发实现的简单计算器功能【附完整demo源码下载】Android中使用GridLayout网格布局来制作简单的计算器Appandroid计算器代码示例分享Android实战教程第一篇之最简单的计算器android studio实现计算器


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯