p.s.
java不是我常用的语言,写得可能不咋地 额…因为习惯问题所以会说函数而不是方法
思路
- 计算器(应该)有2个数,这里命名为number1和number2,输入步骤是:number1->运算符(也许叫这个???)->number2,当前输入用input_index表示
- 这里用String类型的原因是为DEL按钮(假装有)做铺垫,因为整数的退格还好说,但是如果是小数就会非常麻烦,所以干脆转换为String,直接截取字符串就行了
- 因为对每个事件写相同代码太麻烦了,所以用函数
- 看起来是个超级长,超级难理解的函数
public void onNumberButtonClick(String num) {
if ((input_index == 0) || (input_index == 1)) {
input_index = 1;
if (number1 == "0") {
if (num != "00") {
if (num == ".") {
if (!number1.contains(".")) {
number1 += num;
}
} else {
number1 = num;
}
}
} else {
if (num == ".") {
if (!number1.contains(".")) {
number1 += num;
}
} else {
number1 = num;
}
}
} else if (input_index == 2) {
if (number2 == "0") {
if (num != "00") {
if (num == ".") {
if (!number2.contains(".")) {
number2 += num;
}
} else {
number2 = num;
}
}
} else {
if (num == ".") {
if (!number2.contains(".")) {
number2 += num;
}
} else {
number2 = num;
}
}
}
update();
}
按钮中有2个特殊按钮——“.”和“00” 当number1为“0”的时候,输入00就会变成“00”,所以不行(不知道会不会报错,但是不能冒这个险) 如果通过了,就进入下一个——. 如果不加以限制,就可能变成“0…”,这很明显不对
之后就没什么好讲的了,只要不忘记调用update()函数就行
最后成品
代码
MainActivity.java
package com.Diamond.Calculator.ui.activity;
import android.os.Bundle;
import com.Diamond.Calculator.R;
import com.Diamond.Calculator.common.activity.BaseActivity;
import android.widget.Button;
import android.view.*;
import android.widget.TextView;
import android.util.Log;
import java.lang.Boolean;
import java.lang.Object;
import java.util.Random;
import java.time.*;
import android.util.TimeUtils;
import java.security.Timestamp;
public class MainActivity extends BaseActivity {
private String number1;
private String number2;
private String ans;
private int input_index;
private String op;
private TextView textDisplay;
private Button numberButton0;
private Button numberButtonPoint;
private Button numberButton00;
private Button numberButton1;
private Button numberButton2;
private Button numberButton3;
private Button numberButton4;
private Button numberButton5;
private Button numberButton6;
private Button numberButton7;
private Button numberButton8;
private Button numberButton9;
private Button operatorButtonPlus;
private Button operatorButtonMinus;
private Button operatorButtonMultiply;
private Button operatorButtonDivide;
private Button operatorButtonEqual;
private Button randomButton;
private Button ansButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = "0";
number2 = "0";
input_index = 1;
op = new String();
textDisplay = (TextView)findViewById(R.id.activitymainTextDisplay);
numberButton0 = (Button)findViewById(R.id.activitymainNumberButton0);
numberButtonPoint = (Button)findViewById(R.id.activitymainNumberButtonPoint);
numberButton00 = (Button)findViewById(R.id.activitymainNumberButton00);
numberButton1 = (Button)findViewById(R.id.activitymainNumberButton1);
numberButton2 = (Button)findViewById(R.id.activitymainNumberButton2);
numberButton3 = (Button)findViewById(R.id.activitymainNumberButton3);
numberButton4 = (Button)findViewById(R.id.activitymainNumberButton4);
numberButton5 = (Button)findViewById(R.id.activitymainNumberButton5);
numberButton6 = (Button)findViewById(R.id.activitymainNumberButton6);
numberButton7 = (Button)findViewById(R.id.activitymainNumberButton7);
numberButton8 = (Button)findViewById(R.id.activitymainNumberButton8);
numberButton9 = (Button)findViewById(R.id.activitymainNumberButton9);
operatorButtonPlus = (Button)findViewById(R.id.activitymainOperatorButtonPlus);
operatorButtonMinus = (Button)findViewById(R.id.activitymainOperatorButtonMinus);
operatorButtonMultiply = (Button)findViewById(R.id.activitymainOperatorButtonMultiply);
operatorButtonDivide = (Button)findViewById(R.id.activitymainOperatorButtonDivide);
operatorButtonEqual = (Button)findViewById(R.id.activitymainOperatorButtonEqual);
randomButton = (Button)findViewById(R.id.activitymainButtonRandom);
ansButton = (Button)findViewById(R.id.activitymainButtonAns);
numberButton0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(0);
}
});
numberButtonPoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(".");
}
});
numberButton00.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick("00");
}
});
numberButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(1);
}
});
numberButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(2);
}
});
numberButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(3);
}
});
numberButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(4);
}
});
numberButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(5);
}
});
numberButton6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(6);
}
});
numberButton7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(7);
}
});
numberButton8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(8);
}
});
numberButton9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onNumberButtonClick(9);
}
});
operatorButtonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onOperatorButtonClick("+");
}
});
operatorButtonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onOperatorButtonClick("-");
}
});
operatorButtonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onOperatorButtonClick("*");
}
});
operatorButtonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onOperatorButtonClick("/");
}
});
operatorButtonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onOperatorButtonClick("=");
}
});
randomButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
input_index = 0;
Random r = new Random(System.currentTimeMillis());
ans = new Double(r.nextDouble()).toString();
reset(false, 0);
update();
}
});
ansButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(input_index == 0) {
reset(false,1);
number1 = ans;
} else if (input_index == 1) {
number1 = ans;
} else {
number2 = ans;
}
update();
}
});
}
public void onNumberButtonClick(int num) {
if ((input_index == 0) || (input_index == 1)) {
input_index = 1;
if (number1 == "0") {
number1 = new Integer(num).toString();
} else {
number1 += num;
}
} else if (input_index == 2) {
if (number2 == "0") {
number2 = new Integer(num).toString();
} else {
number2 += num;
}
}
update();
}
public void onNumberButtonClick(String num) {
if ((input_index == 0) || (input_index == 1)) {
input_index = 1;
if (number1 == "0") {
if (num != "00") {
if (num == ".") {
if (!number1.contains(".")) {
number1 += num;
}
} else {
number1 = num;
}
}
} else {
if (num == ".") {
if (!number1.contains(".")) {
number1 += num;
}
} else {
number1 = num;
}
}
} else if (input_index == 2) {
if (number2 == "0") {
if (num != "00") {
if (num == ".") {
if (!number2.contains(".")) {
number2 += num;
}
} else {
number2 = num;
}
}
} else {
if (num == ".") {
if (!number2.contains(".")) {
number2 += num;
}
} else {
number2 = num;
}
}
}
update();
}
public void onOperatorButtonClick(String oper) {
if (oper != "=") {
op = oper;
if (input_index == 1) {
input_index = 2;
number2 = "0";
} else if (input_index == 2) {
solveResult();
number1 = ans;
number2 = "0";
} else if (input_index == 0) {
input_index = 2;
number1 = ans;
number2 = "0";
}
} else {
solveResult();
reset(false,0);
}
update();
}
public void solveResult() {
double result = new Double(0);
double num1 = new Double(number1);
double num2 = new Double(number2);
if (op == "+") {
result = num1 + num2;
}
else if (op == "-") {
result = num1 - num2;
}
else if (op == "*") {
result = num1 * num2;
}
else if (op == "/") {
result = num1 / num2;
}
ans = ((Double)result).toString();
}
public void update() {
if (input_index == 1) {
textDisplay.setText(number1);
} else if (input_index == 2) {
textDisplay.setText(number1 + op + number2);
} else {
textDisplay.setText(ans);
}
int textLength = textDisplay.getText().length();
if (textLength <= 20) {
textDisplay.setTextSize(50);
} else if (textLength > 20 && textLength <= 30) {
textDisplay.setTextSize(40);
} else if (textLength > 30) {
textDisplay.setTextSize(30);
}
}
public void reset(Boolean reset_ans, int ii) {
number1 = "0";
number2 = "0";
if (reset_ans) {
ans = "0";
}
input_index = ii;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="0"
android:id="@+id/activitymainTextDisplay"/>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="7"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="8"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="9"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton9"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="+"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/activitymainOperatorButtonPlus"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="4"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="5"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="6"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton6"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="-"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainOperatorButtonMinus"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="1"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="2"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="3"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="*"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainOperatorButtonMultiply"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="0"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="."
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainNumberButtonPoint"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="00"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainNumberButton00"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="/"
android:layout_weight="1.0"
android:textSize="30sp"
android:id="@+id/activitymainOperatorButtonDivide"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="random"
android:layout_weight="1.0"
android:id="@+id/activitymainButtonRandom"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="ans"
android:layout_weight="1.0"
android:textSize="20sp"
android:id="@+id/activitymainButtonAns"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="="
android:layout_weight="10"
android:textSize="40sp"
android:id="@+id/activitymainOperatorButtonEqual"/>
</LinearLayout>
</LinearLayout>
|