android studio实现简单的计算器功能
作者:萧丶RY
这篇文章主要为大家详细介绍了android studio实现简单的计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下
先来个效果图:
功能: 满足加减乘除四则运算规则,有回退、清除功能。
下面的代码只是完成基本功能,若添加背景图先看看下面的方法:Android Studio App设置背景图片
1、本地准备好图片,复制它,粘贴进mipmap(drawable)文件夹。
2、在activity_main.xml里添加下面代码,注意添加位置。
3、完成,效果图:
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"><-</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计算器 进行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。