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

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

先来个效果图:

android studio实现简单的计算器功能_第1张图片

功能: 满足加减乘除四则运算规则,有回退、清除功能。
下面的代码只是完成基本功能,若添加背景图先看看下面的方法:

Android Studio App设置背景图片

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

android studio实现简单的计算器功能_第2张图片

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

android studio实现简单的计算器功能_第3张图片

3、完成,效果图:

android studio实现简单的计算器功能_第4张图片

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




 
  
  
  
  
  
  
  


 


 

 

 

  

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


 Calculator
 Settings
 page2

 下一页
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 +
 -
 *
 /
 CE
 <-
 =
 请输入:
 结果:
 .
 计算式

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 
 

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

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

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

你可能感兴趣的