image.png
思路
- 首先设计界面,activity_main.xml
计算器界面,需要:文本框TextView——显示数字;数字、符号按钮Button
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<!--文本标签-->
<TextView
android:id="@+id/text"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:layout_marginLeft="4px"
android:gravity="left"
android:textSize="50dp" />
<Button
android:id="@+id/btnClear"
android:layout_width="353dp"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:background="@color/colorAccent"
android:text="清除"
android:textSize="26sp" />
<Button
android:id="@+id/btn1"
android:text="1"
android:textSize="26sp" />
<Button
android:id="@+id/btn2"
android:text="2"
android:textSize="26sp" />
<Button
android:id="@+id/btn3"
android:text="3"
android:textSize="26sp" />
<Button
android:id="@+id/btnPlus"
android:background="@color/colorPrimary"
android:text="+"
android:textSize="26sp" />
<Button
android:id="@+id/btn4"
android:text="4"
android:textSize="26sp" />
<Button
android:id="@+id/btn5"
android:text="5"
android:textSize="26sp" />
<Button
android:id="@+id/btn6"
android:text="6"
android:textSize="26sp" />
<Button
android:id="@+id/btnSubtract"
android:background="@color/colorPrimary"
android:text="-"
android:textSize="26sp" />
<Button
android:id="@+id/btn7"
android:text="7"
android:textSize="26sp" />
<Button
android:id="@+id/btn8"
android:text="8"
android:textSize="26sp" />
<Button
android:id="@+id/btn9"
android:text="9"
android:textSize="26sp" />
<Button
android:id="@+id/btnMultiply"
android:background="@color/colorPrimary"
android:text="*"
android:textSize="26sp" />
<Button
android:id="@+id/btnPoint"
android:text="."
android:textSize="26sp" />
<Button
android:id="@+id/btn0"
android:text="0"
android:textSize="26sp" />
<Button
android:id="@+id/btnSum"
android:text="="
android:textSize="26sp" />
<Button
android:id="@+id/btnDivide"
android:background="@color/colorPrimary"
android:text="/"
android:textSize="26sp" />
</GridLayout>
- 接下来是功能的实现,MainActivity.java
给各个按钮注册点击事件、对输入内容的判断和计算等。
package com.example.asus.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
btnClear,btnPlus,btnSubtract,btnMultiply,btnDivide,btnSum,btnPoint;
TextView text;
String str = "";
boolean clr_flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0 = (Button) findViewById(R.id.btn0);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btnClear = (Button) findViewById(R.id.btnClear);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnSubtract = (Button) findViewById(R.id.btnSubtract);
btnMultiply = (Button) findViewById(R.id.btnMultiply);
btnDivide = (Button) findViewById(R.id.btnDivide);
btnPoint = (Button) findViewById(R.id.btnPoint);
btnSum = (Button) findViewById(R.id.btnSum);
text = (TextView) findViewById(R.id.text) ;
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnPlus.setOnClickListener(this);
btnSubtract.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
btnPoint.setOnClickListener(this);
btnSum.setOnClickListener(new click());
}
public void onClick(View v) {
String input=text.getText().toString();
switch (v.getId()){
case R.id.btn0:
case R.id.btn1:
case R.id.btn2:
case R.id.btn3:
case R.id.btn4:
case R.id.btn5:
case R.id.btn6:
case R.id.btn7:
case R.id.btn8:
case R.id.btn9:
case R.id.btnPoint:
if(clr_flag){
clr_flag=false;
str="";
text.setText("");
}
text.setText(input+((Button)v).getText());
break;
case R.id.btnPlus:
case R.id.btnSubtract:
case R.id.btnMultiply:
case R.id.btnDivide:
if(clr_flag){
clr_flag=false;
input="";
text.setText("");
}
text.setText(input + " " + ((Button)v).getText() + " ");
break;
case R.id.btnClear:
text.setText("");
break;
}
}
class click implements OnClickListener{
public void onClick (View v) {
getResult();//调用方法,计算结果
}
}
private void getResult () {
String str1 = text.getText().toString();
if(str1 == null || str1.equals("")){
return;
}
if(!str1.contains(" ")){
return ;
}
if(clr_flag){
clr_flag=false;
return;
}
clr_flag=true;
double result = 0;
// 运算符前的数字
String s1 = str1.substring(0,str1.indexOf(" "));
// 运算符
String op = str1.substring(str1.indexOf(" ")+1,str1.indexOf(" ")+2);
// 运算符后的数字
String s2 = str1.substring(str1.indexOf(" ")+3);
// 判断
// 1. 如果s1、s2都不为空
if(!s1.equals("")&&!s2.equals("")) { //包含小数点运算
double d1 = Double.parseDouble(s1);//则数字都是double类型
double d2 = Double.parseDouble(s2);
if (op.equals("+")) { //如果是 +
result = d1 + d2;
} else if (op.equals("-")) { //如果是 -
result = d1 - d2;
} else if (op.equals("*")) { //如果是 *
result = d1 * d2;
} else if (op.equals("/")) { //如果是 /
if (d2 == 0) { //如果被除数是0
result = 0; //则结果是0
}
else {//否则执行正常是除法运算
result = d1 / d2;
}
}
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//如果是整数类型
int r = (int) result; //都是整形
text.setText(str1 + " = " + r + "");
} else{
text.setText(str1 + " = " + result + "");
}
}
// 如果s1不是空,s2是空
else if(!s1.equals("")&&s2.equals("")){
double d1 = Double.parseDouble(s1);
if (op.equals("+")){
result = d1;
}
if (op.equals("-")) {
result = d1;
}
if (op.equals("*")) {
result = 0;
}
if (op.equals("/")) {
result = 0;
}
if(!s1.contains(".")) {
int res = (int) result;
text.setText(str1 + " = " + res+"");
}else {
text.setText(str1 + " = " + result+"");
}
}
// 如果s1是空,s2不是空
else if(s1.equals("")&& !s2.equals("")){
double d2 = Double.parseDouble(s2);
if(op.equals("+")){
result = 0+d2;
}else if(op.equals("-")){
result = 0-d2;
}else if(op.equals("*")){
result = 0;
}else if(op.equals("/")){
result = 0;
}
if(!s1.contains(".") && !s2.contains(".")){
int res = (int) result;
text.setText(str1 + " = " + res + "");
}else{
text.setText(str1 + " = " + result + "");
}
}else{
text.setText("");
}
}
}
网友评论