软件内容简介:这是一个具有基本计算功能(加减乘除)的计算器。
界面设计(拷屏界面):
代码设计:
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:rowCount="7"
android:columnCount="4"
tools:context=".MainActivity"
android:background="#FFFFFF"
>
<!--输入文本框-->
<TextView
android:id="@+id/tv_expression"
android:layout_gravity="fill"
android:layout_columnSpan="4"
android:gravity="right|bottom"
android:paddingTop="30dp"
android:text=""
android:textSize="60dp"
android:textColor="#FFFFFF"
android:background="#63B8FF"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/>
<TextView
android:id="@+id/tv_result"
android:layout_columnSpan="4"
android:layout_gravity="fill"
android:gravity="top|right"
android:paddingBottom="30dp"
android:text=""
android:textSize="25dp"
android:textColor="#FFFFFF"
android:background="#63B8FF"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/>
<Button
android:id="@+id/btn_clear"
android:background="@null"
android:text="C"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/btn_del"
android:text="DEL"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/btn_divide"
android:text="/"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/btn_multiply"
android:text="*"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/btn_7"
android:text="7"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="8"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:id="@+id/btn_8"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="9"
android:id="@+id/btn_9"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="+"
android:id="@+id/btn_add"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="4"
android:id="@+id/btn_4"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="5"
android:id="@+id/btn_5"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="6"
android:id="@+id/btn_6"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="-"
android:id="@+id/btn_minus"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="3"
android:id="@+id/btn_3"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="2"
android:id="@+id/btn_2"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="1"
android:id="@+id/btn_1"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:text="="
android:id="@+id/btn_equal"
android:layout_gravity="fill_vertical|center_horizontal"
android:layout_rowSpan="2"
android:layout_height="74dp"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="#FFFFFF"
android:background="#ec6c04"
android:textSize="30dp"
android:shadowColor="#FFFFFF"
android:layout_rowWeight="2"
android:layout_columnWeight="1"
/>
<Button
android:text="0"
android:id="@+id/btn_0"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal|center_horizontal"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_rowWeight="1"
android:layout_columnWeight="2"
/>
<Button
android:text="."
android:id="@+id/btn_point"
android:background="@null"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
</GridLayout>
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.al.mycalculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
//MainActivity.java
package com.example.al.mycalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static java.lang.Character.isDigit;
import java.util.Stack;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btn_0;
Button btn_1;
Button btn_2;
Button btn_3;
Button btn_4;
Button btn_5;
Button btn_6;
Button btn_7;
Button btn_8;
Button btn_9;
Button btn_point;
Button btn_clear;
Button btn_del;
Button btn_add;
Button btn_minus;
Button btn_multiply;
Button btn_divide;
Button btn_equal;
TextView tv_result;
TextView tv_expression;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_0 = (Button) findViewById(R.id.btn_0) ;
btn_1 = (Button) findViewById(R.id.btn_1) ;
btn_2 = (Button) findViewById(R.id.btn_2) ;
btn_3 = (Button) findViewById(R.id.btn_3) ;
btn_4 = (Button) findViewById(R.id.btn_4) ;
btn_5 = (Button) findViewById(R.id.btn_5) ;
btn_6 = (Button) findViewById(R.id.btn_6) ;
btn_7 = (Button) findViewById(R.id.btn_7) ;
btn_8 = (Button) findViewById(R.id.btn_8) ;
btn_9 = (Button) findViewById(R.id.btn_9) ;
btn_point = (Button) findViewById(R.id.btn_point) ;
btn_clear = (Button) findViewById(R.id.btn_clear) ;
btn_del = (Button) findViewById(R.id.btn_del) ;
btn_add = (Button) findViewById(R.id.btn_add) ;
btn_minus = (Button) findViewById(R.id.btn_minus) ;
btn_multiply = (Button) findViewById(R.id.btn_multiply) ;
btn_divide = (Button) findViewById(R.id.btn_divide) ;
btn_equal = (Button) findViewById(R.id.btn_equal) ;
tv_expression=(TextView)findViewById(R.id.tv_expression);
tv_result=(TextView)findViewById(R.id.tv_result);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_add.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_equal.setOnClickListener(this);
}
public void onClick(View v){
String str=tv_expression.getText().toString();
boolean flag=true;
switch (v.getId()){
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
tv_expression.setText(str+((Button)v).getText());break;
case R.id.btn_point:
if(str!=null&&!str.equals("")) {
if ('0' <= str.charAt(str.length() - 1) && '9' >= str.charAt(str.length() - 1))
tv_expression.setText(str + ".");
}
break;
case R.id.btn_add:
case R.id.btn_minus:
case R.id.btn_multiply:
case R.id.btn_divide:
if(str!=null&&!str.equals("")) {
if (('0' <= str.charAt(str.length() - 1) && '9' >= str.charAt(str.length() - 1)) || '.' == str.charAt(str.length() - 1))
tv_expression.setText(str + ((Button) v).getText());
}
break;
case R.id.btn_del:
if(str!=null&&!str.equals(""))tv_expression.setText(str.substring(0,str.length()-1));
break;
case R.id.btn_clear:
tv_expression.setText("");
tv_result.setText("");
break;
case R.id.btn_equal:
tv_expression.setText(tv_result.getText().toString());
tv_result.setText("");
flag=false;
break;
}
if(flag)
tv_result.setText(getResult(intosuf(tv_expression.getText().toString())));
}
public String getResult(String sufstr){
if(sufstr==null||sufstr.equals(""))return "";
int strlen=sufstr.length();
Stack<String> st=new Stack<String>();
for(int i=0;i<strlen;i++){
String str="";
if(isDigit(sufstr.charAt(i))){
while(i<strlen&&(isDigit(sufstr.charAt(i))||sufstr.charAt(i)=='.')){
str=str+String.valueOf(sufstr.charAt(i));
i++;
}
i--;
}
else str=str+String.valueOf(sufstr.charAt(i));
double a=0,b=0;
switch(str){
case "#":break;
case "+":
if(!st.empty())
b=Double.parseDouble(st.pop());
if(!st.empty())
a=Double.parseDouble(st.pop());
st.push(Double.toString(a+b));
break;
case "-":
if(!st.empty())
b=Double.parseDouble(st.pop());
if(!st.empty())
a=Double.parseDouble(st.pop());
st.push(Double.toString(a-b));
break;
case "*":
if(!st.empty())
b=Double.parseDouble(st.pop());
if(!st.empty())
a=Double.parseDouble(st.pop());
st.push(Double.toString(a*b));
break;
case "/":
if(!st.empty())
b=Double.parseDouble(st.pop());
if(!st.empty())
a=Double.parseDouble(st.pop());
st.push(Double.toString(a/b));
break;
default:st.push(str);
}
}
if(!st.empty()) return doubleTrans(Double.parseDouble(st.pop()));
else return "";
}
public String intosuf(String instr){
if(instr==null||instr.equals(""))return "";
String sufstr="";
String str=null;
Stack<String> st=new Stack<String>();
int strlen=instr.length();
if(instr.charAt(strlen-1)!='.'&&!isDigit(instr.charAt(strlen-1))){
instr=instr.substring(0, strlen-1);
strlen--;
}
for(int i=0;i<strlen;i++){
if(isDigit(instr.charAt(i))){str="";
while(i<strlen&&(isDigit(instr.charAt(i))||instr.charAt(i)=='.')){
str=str+String.valueOf(instr.charAt(i));
i++;
}
i--;}
else str=String.valueOf(instr.charAt(i));
switch(str){
case "*":
case "/":if(st.empty()){st.push(str);break;}
else {
if(st.peek().equals("*")||st.peek().equals("/"))sufstr=sufstr+st.pop();
st.push(str);break;}
case "+":
case "-":if(st.empty()){st.push(str);break;}
else {
if(st.peek().equals("*")||st.peek().equals("/"))sufstr=sufstr+st.pop();
if(!st.empty()&&(st.peek().equals("+")||st.peek().equals("-")))sufstr=sufstr+st.pop();
st.push(str);break;
}
default:sufstr=sufstr+str+"#";break;
}
}
while(!st.empty())sufstr=sufstr+st.pop();
return sufstr;
}
public String doubleTrans(double num){
if(num % 1.0 == 0){
return String.valueOf((long)num);
}
return String.valueOf(num);
}
}
软件操作流程:
按数字键和加减乘除,最上面的text view会显示算式,下面的text view会马上显示结果,按等于号,上面的算式会变成下面的结果,而下面的结果会变成没有内容。
按C键会删除所有内容,按del键会删除算式最右边的一个字符。
难点(或遇到的问题)和解决方案:
难点一:用GridLayout编写界面时出现每一个按钮的大小和位置的不均匀对称。
解决方案:上网查一下,发现每个按钮调一下下面这三个属性就可以了
android:layout_gravity="center_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
难点二:当text view的字数超过一行所能容纳的个数时,text view会变大然后影响下面按钮的位置。
解决方案:用下面这些属性把text view设置成只能有一行,多出来的用省略号显示。
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
难点三:实现输入算式马上出结果。
解决方案:在每一个按钮(除了等于号)触发时把算式的结果计算出来并显示在text view上。