只是记录一下代码,随意取用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
</LinearLayout>
<!--写一个计算器的layout-->
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/white"
android:gravity="right"
android:padding="10dp"
android:text=" "
android:textColor="@color/black"
android:textSize="30sp"
tools:ignore="DuplicateIds" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="C"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_del"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@color/white"
android:text="Del"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/btn_div"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="/"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_mul"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="*"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<!--计算器的数字按键-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="7"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="8"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="9"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_sub"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="-"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="4"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="5"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="6"
android:textColor="@color/white"
android:textSize="30sp" />
<!-- 补充符号-->
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="+"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="1"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="2"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="3"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_equal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="="
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<!-- 百分号,0,小数点按键-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_percent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="%"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="0"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_point"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="."
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_none"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="B"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
然后是MainActivity.java
package com.zdb.hwfrist;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
//导入数据框
import android.widget.Button;
import android.widget.TextView;
import java.util.Stack;
public class MainActivity extends AppCompatActivity {
//创建button对象
Button btn_none,btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_percent, btn_del, btn_point, btn_add, btn_sub, btn_mul, btn_div, btn_equal, btn_clear;
//创建TextView对象
TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化对象
btn_0 = findViewById(R.id.btn_0);
btn_1 = findViewById(R.id.btn_1);
btn_2 = findViewById(R.id.btn_2);
btn_3 = findViewById(R.id.btn_3);
btn_4 = findViewById(R.id.btn_4);
btn_5 = findViewById(R.id.btn_5);
btn_6 = findViewById(R.id.btn_6);
btn_7 = findViewById(R.id.btn_7);
btn_8 = findViewById(R.id.btn_8);
btn_9 = findViewById(R.id.btn_9);
btn_percent = findViewById(R.id.btn_percent);
btn_add = findViewById(R.id.btn_add);
btn_sub = findViewById(R.id.btn_sub);
btn_mul = findViewById(R.id.btn_mul);
btn_div = findViewById(R.id.btn_div);
btn_del = findViewById(R.id.btn_del);
btn_equal = findViewById(R.id.btn_equal);
btn_point = findViewById(R.id.btn_point);
btn_clear = findViewById(R.id.btn_clear);
tv_result = findViewById(R.id.tv_result);
btn_none = findViewById(R.id.btn_none);
// 给按钮添加点击事件
btn_0.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "0");
});
btn_1.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "1");
});
btn_2.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "2");
});
btn_3.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "3");
});
btn_4.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "4");
});
btn_5.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "5");
});
btn_6.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "6");
});
btn_7.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "7");
});
btn_8.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "8");
});
btn_9.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "9");
});
btn_point.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + ".");
});
btn_clear.setOnClickListener(v -> {
tv_result.setText(" ");
});
btn_none.setOnClickListener(v -> {
tv_result.setText("叫你手贱,点我干什么");
});
btn_del.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
if (str_out.length() == 1) {
tv_result.setText(" ");
} else {
tv_result.setText(str_out.substring(0, str_out.length() - 1));
}
});
btn_add.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "+");
});
btn_sub.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "-");
});
btn_mul.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "*");
});
btn_div.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "/");
});
btn_percent.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "%");
});
btn_equal.setOnClickListener(v -> {
String str = tv_result.getText().toString();
// 如果str是中文,就不执行
if (str.contains("叫")) {
return;
}
// 如果str是空的,就不执行
if (str.equals(" ")) {
return;
}
// 如果str是以运算符结尾,就不执行
if (str.endsWith("+") || str.endsWith("-") || str.endsWith("*") || str.endsWith("/")) {
tv_result.setText(str+"好好写");
return;
}
str="1*"+str;
// 如果str有%,则在%后面加一个1
if (str.contains("%")) {
str = str.replace("%", "%0");
}
Object result = calculate(str);
System.out.println(result);
// 打印result
System.out.println(result);
// 如果小数点后面是0,就不显示
if (result.toString().endsWith(".0")) {
tv_result.setText(result.toString().substring(0, result.toString().length() - 2));
} else {
tv_result.setText(result + "");
}
});
}
public static double calculate(String input) {
String[] parts = input.split("(?<=[-+*/%])|(?=[-+*/%])");
Stack<Double> numbers = new Stack<>();
Stack<Character> operators = new Stack<>();
for (String part : parts) {
part = part.trim();
if (part.isEmpty()) {
continue;
}
char firstChar = part.charAt(0);
if (Character.isDigit(firstChar)) {
double num = Double.parseDouble(part);
numbers.push(num);
} else {
while (!operators.isEmpty() && shouldPopOperator(operators.peek(), firstChar)) {
char op = operators.pop();
double num2 = numbers.pop();
double num1 = numbers.pop();
double result = calculateOneOperation(num1, num2, op);
numbers.push(result);
}
operators.push(firstChar);
}
}
while (!operators.isEmpty()) {
char op = operators.pop();
double num2 = numbers.pop();
double num1 = numbers.pop();
double result = calculateOneOperation(num1, num2, op);
numbers.push(result);
}
return numbers.pop();
}
private static boolean shouldPopOperator(char op1, char op2) {
if (op1 == '(' || op1 == ')') {
return false;
}
int precedence1 = getOperatorPrecedence(op1);
int precedence2 = getOperatorPrecedence(op2);
return precedence1 >= precedence2;
}
private static int getOperatorPrecedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '%':
return 3;
default:
return 0;
}
}
private static double calculateOneOperation(double num1, double num2, char op) {
switch (op) {
case '%':
return num1 * 0.01;
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
throw new IllegalArgumentException("Invalid operator: " + op);
}
}
}
标签:tv,setText,studio,result,str,计算器,android,btn,out
From: https://www.cnblogs.com/bertin/p/17320806.html