Android_计算器的实现
Android_计算器的实现
源码:
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
android:id="@+id/two">
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:layout_marginLeft="4px"
android:textSize="50dip"/>
<Button
android:id="@+id/cleanButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:text="清除"
android:textSize="26sp"/>
<Button android:id="@+id/button1" android:text="1" android:textSize="26sp"/>
<Button android:id="@+id/button2" android:text="2" android:textSize="26sp"/>
<Button android:id="@+id/button3" android:text="3" android:textSize="26sp"/>
<Button android:id="@+id/buttonSymbol1" android:text="+" android:textSize="26sp"/>
<Button android:id="@+id/button4" android:text="4" android:textSize="26sp"/>
<Button android:id="@+id/button5" android:text="5" android:textSize="26sp"/>
<Button android:id="@+id/button6" android:text="6" android:textSize="26sp"/>
<Button android:id="@+id/buttonSymbol2" android:text="-" android:textSize="26sp"/>
<Button android:id="@+id/button7" android:text="7" android:textSize="26sp"/>
<Button android:id="@+id/button8" android:text="8" android:textSize="26sp"/>
<Button android:id="@+id/button9" android:text="9" android:textSize="26sp"/>
<Button android:id="@+id/buttonSymbol3" android:text="*" android:textSize="26sp"/>
<Button android:id="@+id/buttonSymbol4" android:text="." android:textSize="26sp"/>
<Button android:id="@+id/button0" android:text="0" android:textSize="26sp"/>
<Button android:id="@+id/buttonSymbol5" android:text="=" android:textSize="26sp"/>
<Button android:id="@+id/buttonSymbol6" android:text="/" android:textSize="26sp"/>
</GridLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java 文件
package com.example.me.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
TextView resultText;
Button button0, button1, button2, button3, button4, button5, button6,button7,button8,button9,
buttonSymbol, buttonSymbo2,buttonSymbo3,buttonSymbo4,buttonSymbo5,buttonSymbo6;
Button cleanButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//清除按钮的点击事件
cleanButton = (Button)findViewById(R.id.cleanButton);
cleanButton.setOnClickListener(new cleanClick());
resultText = (TextView)findViewById(R.id.resultText);
resultText.setText("0");
//获取 button 组件
button0 = (Button)findViewById(R.id.button0);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button5 = (Button)findViewById(R.id.button5);
button6 = (Button)findViewById(R.id.button6);
button7 = (Button)findViewById(R.id.button7);
button8 = (Button)findViewById(R.id.button8);
button9 = (Button)findViewById(R.id.button9);
buttonSymbol = (Button)findViewById(R.id.buttonSymbol1);
buttonSymbo2 = (Button)findViewById(R.id.buttonSymbol2);
buttonSymbo3 = (Button)findViewById(R.id.buttonSymbol3);
buttonSymbo4 = (Button)findViewById(R.id.buttonSymbol4);
buttonSymbo5 = (Button)findViewById(R.id.buttonSymbol5);
buttonSymbo6 = (Button)findViewById(R.id.buttonSymbol6);
//绑定点击事件
button0.setOnClickListener(new numClick());
button1.setOnClickListener(new numClick());
button2.setOnClickListener(new numClick());
button3.setOnClickListener(new numClick());
button4.setOnClickListener(new numClick());
button5.setOnClickListener(new numClick());
button6.setOnClickListener(new numClick());
button7.setOnClickListener(new numClick());
button8.setOnClickListener(new numClick());
button9.setOnClickListener(new numClick());
buttonSymbol.setOnClickListener(new numClick());
buttonSymbo2.setOnClickListener(new numClick());
buttonSymbo3.setOnClickListener(new numClick());
buttonSymbo4.setOnClickListener(new numClick());
buttonSymbo5.setOnClickListener(new sumClick());
buttonSymbo6.setOnClickListener(new numClick());
}
//计算器按钮的点击事件
class numClick implements View.OnClickListener{
public void onClick(View v){
String str = resultText.getText().toString();
Button btn = (Button)findViewById(v.getId());
String result;
//如果值为“0”,替换值;
if(str == "0"){
result = btn.getText().toString();
}else{
result = str + btn.getText().toString();
}
//显示结果
resultText.setText(result);
}
}
//清除事件
class cleanClick implements View.OnClickListener{
public void onClick(View v) {
resultText.setText("0");
}
}
//计算事件
class sumClick implements View.OnClickListener{
public void onClick(View v){
double result = this.count();
System.out.print(result);
resultText.setText(String.valueOf(result));
}
//计算逻辑,求表达式的值
public double count(){
//获取表达式
String expression = resultText.getText().toString();
double result=0;//存储计算结果
double tNum=1,lowNum=0.1,num=0;
char tmp=0;//表达式的字符
int operate = 1; //识别+-*/符号,为+时为正数,为-时为负数,为×时为-2或2,为/时为3或-3;
boolean point = false;
//遍历表达式
for(int i=0;i<expression.length();i++){
tmp = expression.charAt(i);
if(tmp=='.') { //因为可能出现小数,此处进行判断是否有小数出现
point = true;
lowNum = 0.1;
}else if(tmp=='+'||tmp=='-'){
if(operate!=3&&operate!=-3){ //此处判断通用,适用于+-*
tNum *= num;
}else{ //计算/
tNum /= num;
}
if(operate<0){ //累加入最终的结果
result -= tNum;
}else{
result += tNum;
}
operate = tmp=='+'?1:-1;
num = 0;
tNum = 1;
point = false;
}else if(tmp=='*'){
if(operate!=3&&operate!=-3){
tNum *= num;
}else{
tNum /= num;
}
operate = operate<0?-2:2;
point = false;
num = 0;
}else if(tmp=='/'){
if(operate!=3&&operate!=-3){
tNum *= num;
}else{
tNum /= num;
}
operate = operate<0?-3:3;
point = false;
num = 0;
}else{
//读取expression中的每个数字,doube型
if(!point){
num = num*10+tmp-'0';
}else{
num += (tmp-'0')*lowNum;
lowNum*=0.1;
}
}
}
//循环遍历结束,计算最后一个运算符后面的数
if(operate!=3&&operate!=-3){
tNum *= num;
}else{
tNum /= num;
}
if(operate<0){
result -= tNum;
}else{
result += tNum;
}
//返回最后的结果
return result;
}
}
}
效果图:
日志
按照书本上的例子,使用 table 布局,写好界面。
在 MainActivity.java 文件,定义了“清除”按钮的点击事件,除等号之外计算器按钮的点击事件,通过 findViewById 获取元素,使用 setOnClickListener 绑定监听器。
个人认为,计算器的计算表达式的逻辑是最难的部分,上网先找了一些解决方案,然后就完成了计算表达式的函数。