欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

android studio实现简单的计算器功能

程序员文章站 2022-11-23 10:52:56
本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下先来个效果图:功能: 满足加减乘除四则运算规则,有回退、清除功能。下面的代码只是完成基本功能,若添加背景图...

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

先来个效果图:

android studio实现简单的计算器功能

功能: 满足加减乘除四则运算规则,有回退、清除功能。

下面的代码只是完成基本功能,若添加背景图先看看下面的方法:android studio app设置背景图片

1、本地准备好图片,复制它,粘贴进mipmap(drawable)文件夹。

android studio实现简单的计算器功能

2、在activity_main.xml里添加下面代码,注意添加位置。

android studio实现简单的计算器功能

3、完成,效果图:

android studio实现简单的计算器功能

content_main.xml文件(页面布局,content_main.xml代码包含在activity_main.xml文件中):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/next"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:layout_behavior="@string/appbar_scrolling_view_behavior"
 tools:context=".mainactivity"
 tools:showin="@layout/activity_main">

 <!--<textview-->
 <!--android:layout_width="wrap_content"-->
 <!--android:layout_height="wrap_content"-->
 <!--android:text="hello world!"-->
 <!--app:layout_constraintbottom_tobottomof="parent"-->
 <!--app:layout_constraintleft_toleftof="parent"-->
 <!--app:layout_constraintright_torightof="parent"-->
 <!--app:layout_constrainttop_totopof="parent" />-->


 <linearlayout
 android:layout_width="fill_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >


 <edittext
 android:id="@+id/input"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:editable="false"
 android:hint="@string/shuru" />

 <edittext
 android:id="@+id/output"
 android:layout_width="match_parent"
 android:layout_height="70dp"
 android:layout_gravity="center"
 android:editable="true"
 android:gravity="right"
 android:hint="@string/shuchu" />

 <relativelayout
 android:layout_width="350dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center">

 <button
  android:id="@+id/clear"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentleft="true"
  android:text="@string/clear"
  android:textsize="40sp" />

 <button
  android:id="@+id/back"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_torightof="@id/clear"
  android:text="@string/back"
  android:textsize="40sp" />

 <button
  android:id="@+id/divide"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_torightof="@id/back"
  android:text="@string/divide"
  android:textsize="40sp" />

 <button
  android:id="@+id/multiply"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_torightof="@id/divide"
  android:text="@string/multiply"
  android:textsize="40sp" />

 <button
  android:id="@+id/seven"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentleft="true"
  android:layout_below="@id/clear"
  android:text="@string/seven"
  android:textsize="40sp" />

 <button
  android:id="@+id/eight"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_torightof="@id/seven"
  android:layout_below="@id/divide"
  android:text="@string/eight"
  android:textsize="40sp" />

 <button
  android:id="@+id/nine"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/multiply"
  android:layout_torightof="@id/eight"
  android:text="@string/nine"
  android:textsize="40sp" />

 <button
  android:id="@+id/subtract"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentright="true"
  android:layout_below="@id/multiply"
  android:layout_torightof="@id/nine"
  android:text="@string/subtract"
  android:textsize="40sp" />

 <button
  android:id="@+id/four"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentleft="true"
  android:layout_below="@id/seven"
  android:text="@string/four"
  android:textsize="40sp" />

 <button
  android:id="@+id/five"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/eight"
  android:layout_torightof="@id/four"
  android:text="@string/five"
  android:textsize="40sp" />

 <button
  android:id="@+id/six"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/nine"
  android:layout_torightof="@id/five"
  android:text="@string/six"
  android:textsize="40sp" />

 <button
  android:id="@+id/add"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentright="true"
  android:layout_below="@id/subtract"
  android:layout_torightof="@id/six"
  android:text="@string/add"
  android:textsize="40sp" />

 <button
  android:id="@+id/one"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentleft="true"

  android:layout_below="@id/four"
  android:text="@string/one"
  android:textsize="40sp" />

 <button
  android:id="@+id/two"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/five"
  android:layout_torightof="@id/one"
  android:text="@string/two"
  android:textsize="40sp" />

 <button
  android:id="@+id/three"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/six"
  android:layout_torightof="@id/two"
  android:text="@string/three"
  android:textsize="40sp" />

 <button
  android:id="@+id/result"
  android:layout_width="wrap_content"
  android:layout_height="146dp"
  android:layout_alignparentright="true"
  android:layout_below="@id/add"
  android:layout_torightof="@id/three"
  android:text="@string/result"
  android:textsize="40sp" />


 <button
  android:id="@+id/zero"
  android:layout_width="175dp"
  android:layout_height="wrap_content"
  android:layout_alignparentleft="true"
  android:layout_below="@id/one"
  android:text="@string/zero"
  android:textsize="40sp" />

 <button
  android:id="@+id/dot"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/three"
  android:layout_torightof="@id/zero"
  android:text="@string/dot"
  android:textsize="40sp" />

 </relativelayout>

 </linearlayout>


</android.support.constraint.constraintlayout>

strings.xml(content_main.xml代码中的一些变量在此代码中定义的):

<resources>
 <string name="app_name">calculator</string>
 <string name="action_settings">settings</string>
 <string name="title_activity_page2">page2</string>

 <string name="next">下一页</string>
 <string name="zero">0</string>
 <string name="one">1</string>
 <string name="two">2</string>
 <string name="three">3</string>
 <string name="four">4</string>
 <string name="five">5</string>
 <string name="six">6</string>
 <string name="seven">7</string>
 <string name="eight">8</string>
 <string name="nine">9</string>
 <string name="add">+</string>
 <string name="subtract">-</string>
 <string name="multiply">*</string>
 <string name="divide">/</string>
 <string name="clear">ce</string>
 <string name="back">&#60;-</string>
 <string name="result">=</string>
 <string name="shuru">请输入:</string>
 <string name="shuchu">结果:</string>
 <string name="dot">.</string>
 <string name="resulttext">计算式</string>

</resources>

mainactivity.java(计算器中实现计算功能的核心代码):

package com.example.dell.calculator;

import android.content.intent;
import android.os.bundle;
import android.support.design.widget.floatingactionbutton;
import android.support.design.widget.snackbar;
import android.support.v7.app.appcompatactivity;
import android.support.v7.widget.toolbar;
import android.view.view;
import android.view.menu;
import android.view.menuitem;

import android.app.activity;
import android.content.context;
import android.view.view.onclicklistener;
import android.widget.checkbox;
import android.widget.edittext;
import android.widget.button;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;

import java.text.decimalformat;
import java.text.numberformat;
import java.util.regex.matcher;
import java.util.regex.pattern;


public class mainactivity extends appcompatactivity {

 private edittext output = null;
 private edittext input = null;
 private button btn0 = null;
 private button btn1 = null;
 private button btn2 = null;
 private button btn3 = null;
 private button btn4 = null;
 private button btn5 = null;
 private button btn6 = null;
 private button btn7 = null;
 private button btn8 = null;
 private button btn9 = null;
 private button btnadd = null;
 private button btnsubtract = null;
 private button btnmultiply = null;
 private button btndivide = null;
 private button btnclear = null;
 private button btnback = null;
 private button btndot = null;
 private button btnresult = null;

 private string text = "";//保存输入的数字和符号
 private double result = 0.0;//输出结果

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);
 setsupportactionbar(toolbar);

 output = (edittext) findviewbyid(r.id.output);
 input = (edittext) findviewbyid(r.id.input);

 btn0 = (button) findviewbyid(r.id.zero);
 btn1 = (button) findviewbyid(r.id.one);
 btn2 = (button) findviewbyid(r.id.two);
 btn3 = (button) findviewbyid(r.id.three);
 btn4 = (button) findviewbyid(r.id.four);
 btn5 = (button) findviewbyid(r.id.five);
 btn6 = (button) findviewbyid(r.id.six);
 btn7 = (button) findviewbyid(r.id.seven);
 btn8 = (button) findviewbyid(r.id.eight);
 btn9 = (button) findviewbyid(r.id.nine);
 btnadd = (button) findviewbyid(r.id.add);
 btnsubtract = (button) findviewbyid(r.id.subtract);
 btnmultiply = (button) findviewbyid(r.id.multiply);
 btndivide = (button) findviewbyid(r.id.divide);
 btnclear = (button) findviewbyid(r.id.clear);
 btnback = (button) findviewbyid(r.id.back);
 btndot = (button) findviewbyid(r.id.dot);
 btnresult = (button) findviewbyid(r.id.result);

 //设置按钮侦听事件
 btn0.setonclicklistener(listener);
 btn1.setonclicklistener(listener);
 btn2.setonclicklistener(listener);
 btn3.setonclicklistener(listener);
 btn4.setonclicklistener(listener);
 btn5.setonclicklistener(listener);
 btn6.setonclicklistener(listener);
 btn7.setonclicklistener(listener);
 btn8.setonclicklistener(listener);
 btn9.setonclicklistener(listener);

 btnadd.setonclicklistener(listener);
 btnsubtract.setonclicklistener(listener);
 btnmultiply.setonclicklistener(listener);
 btndivide.setonclicklistener(listener);
 btnclear.setonclicklistener(listener);
 btnback.setonclicklistener(listener);
 btndot.setonclicklistener(listener);
 btnresult.setonclicklistener(listener);


 floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab);
 fab.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view view) {
  snackbar.make(view, "replace with your own action", snackbar.length_long)
   .setaction("action", null).show();
  }
 });
 }

 @override
 public boolean oncreateoptionsmenu(menu menu) {
 // inflate the menu; this adds items to the action bar if it is present.
 getmenuinflater().inflate(r.menu.menu_main, menu);
 return true;
 }

 @override
 public boolean onoptionsitemselected(menuitem item) {
 // handle action bar item clicks here. the action bar will
 // automatically handle clicks on the home/up button, so long
 // as you specify a parent activity in androidmanifest.xml.
 int id = item.getitemid();

 //noinspection simplifiableifstatement
 if (id == r.id.action_settings) {
  return true;
 }

 return super.onoptionsitemselected(item);
 }


 //public void onclicknext(view view) {
 // intent intent = new intent(this,page2.class);
 // startactivity(intent);
 // }


 private onclicklistener listener = new onclicklistener() {

 public void onclick(view v) {
  // todo auto-generated method stub
  switch (v.getid()) {
  //输入数字
  case r.id.zero:
   num(0);
   break;
  case r.id.one:
   num(1);
   break;
  case r.id.two:
   num(2);
   break;
  case r.id.three:
   num(3);
   break;
  case r.id.four:
   num(4);
   break;
  case r.id.five:
   num(5);
   break;
  case r.id.six:
   num(6);
   break;
  case r.id.seven:
   num(7);
   break;
  case r.id.eight:
   num(8);
   break;
  case r.id.nine:
   num(9);
   break;

  case r.id.dot:
  dot();
  break;
  //执行运算
  case r.id.add:
   add();
   break;
  case r.id.subtract:
   sub();
   break;
  case r.id.multiply:
   multiply();
   break;
  case r.id.divide:
   divide();
   break;
  case r.id.clear:
   clear();
   break;
  case r.id.back:
   back();
   break;
  //计算结果
  case r.id.result:
   result();
   break;

  default:
   break;

  }
  input.settext(text);
  output.settext(string.valueof(result));

 }
 };

 private void num(int i) {
 // todo auto-generated method stub
 text = text + string.valueof(i);
 }

 private void dot() {
 // todo auto-generated method stub

 text = text + ".";
 }

 private void clear() {
 // todo auto-generated method stub

 text = "";
 result = null;
 input.settext("");
 output.settext("");

 }

 private void back() {
 // todo auto-generated method stub

 string str = text.substring(0, text.length()-1);
 text = str;

 }

 private void add() {
 // todo auto-generated method stub

 text += "+";

 }

 private void sub() {
 // todo auto-generated method stub

 text += "-";

 }

 private void multiply() {
 // todo auto-generated method stub

 text += "*";

 }

 private void divide() {
 // todo auto-generated method stub

 text += "/";
 }

 //计算输出结果
 private void result() {
 // todo auto-generated method stub
 result = testoperation(text);
 }


 public double testoperation(string s){
 //分割字符然后放进数组
 string s1 =s.replace("+","-");
 string[] str = s1.split("-");
 double total1=0;
 //先遍历数组,把里面的乘除结果算出来
 for(string str1:str){
  if(str1.contains("*")||str1.contains("/")){
  double total = 0;
  for(int i =0;i<str1.length();){
   int count =1;
   a:for(int j =i+1;j<str1.length();j++){
   char c =str1.charat(j);
   if(c=='*'||c=='/'){
    break a;
   }else{
    count++;
   }
   }

   //将数字截取出来
   string s2 =str1.substring(i,i+count);
   double d = double.parsedouble(s2);
   if(i==0){
   total = d;
   }else{
   char c1 = str1.charat(i-1);
   if(c1=='*'){
    total*=d;
   }else if(c1=='/'){
    //如果除数为0,直接返回null;
    if(d == 0)
    return null;
    total/=d;
   }
   }
   i+=count+1;
  }
  s= s.replace(str1, total+"");
  }
 }
 //进行加减运算
 for(int i =0;i<s.length();i++){
  int count =1;
  a:for(int j=i+1;j<s.length();j++){
  char c = s.charat(j);
  if(c=='+'||c=='-'){
   break a;
  }else{
   count++;
  }
  }
  string s3= s.substring(i,i+count);
  double d2 = double.parsedouble(s3);
  if(i==0){
  total1 = d2;
  }else{
  char c = s.charat(i-1);
  if(c=='+'){
   total1+=d2;
  }else if(c=='-'){
   total1-=d2;
  }
  }
  i+=count;
 }
 return total1;
 }

}

更多计算器功能实现,请点击专题: 计算器功能汇总 进行学习

关于android计算器功能的实现,查看专题:android计算器 进行学习。

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