记录一下,还有点小BUG和多余的代码,放上来存储一下2020-5-9
package com.example.myapplication2020_3_17.student05;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.AsyncListUtil;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.myapplication2020_3_17.R;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Student05Activity06 extends AppCompatActivity implements View.OnClickListener {
private Button clear,b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,add,sbc,mla,chu,point,eq;
private EditText tv;
private StringBuilder show_equation1=new StringBuilder();//显示运算式
private StringBuilder show_equation2=new StringBuilder();//显示运算式
private ArrayList calculate_equation;//计算式
private ArrayList calculate_equation1;//计算式
public String t1,t2,t3;
public BigDecimal d1,d2,d3,d4,d5,d6,d7,d8,r;
private int signal=0;//为0 时表示刚输入状态;为1 时表示当前在输出结果上继续输入
//计算器界面
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student0506);
show_equation1=new StringBuilder();
calculate_equation = new ArrayList<String>();
calculate_equation1 = new ArrayList<String>();
clear = (Button) findViewById(R.id.clear);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
b7 = (Button) findViewById(R.id.b7);
b8 = (Button) findViewById(R.id.b8);
b9 = (Button) findViewById(R.id.b9);
b0 = (Button) findViewById(R.id.b0);
add = (Button) findViewById(R.id.add);
sbc = (Button) findViewById(R.id.sbc);
mla = (Button) findViewById(R.id.mla);
chu = (Button) findViewById(R.id.chu);
tv = (EditText) findViewById(R.id.tv);
point = (Button) findViewById(R.id.point);
eq = (Button) findViewById(R.id.eq);
tv.setCursorVisible(true);
//点击文本框时光标始终在文本末尾
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setSelection(tv.getText().length());
}
});
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b0.setOnClickListener(this);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_equation1.delete(0,show_equation1.length());
tv.setText("0");
}
});
mla.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// show_equation1.append("*");
// tv.setText(show_equation1);
// tv.setSelection(tv.getText().length());
d5 = new BigDecimal(show_equation1.toString());
show_equation1.delete(0,show_equation1.length());
tv.setText("*");
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// show_equation1.append("+");
// tv.setText(show_equation1);
// tv.setSelection(tv.getText().length());
d1 = new BigDecimal(show_equation1.toString());
show_equation1.delete(0,show_equation1.length());
tv.setText("+");
}
});
sbc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// show_equation1.append("-");
// tv.setText(show_equation1);
// tv.setSelection(tv.getText().length());
d3 = new BigDecimal(show_equation1.toString());
show_equation1.delete(0,show_equation1.length());
tv.setText("-");
}
});
chu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// show_equation1.append("/");
// tv.setText(show_equation1);
// tv.setSelection(tv.getText().length());
d7 = new BigDecimal(show_equation1.toString());
show_equation1.delete(0,show_equation1.length());
tv.setText("/");
}
});
eq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t1 = tv.getText().toString();
String[] str=null;
//str = t1.split( "'+'");
//str[0]="1";
if(d1!=null){
d2 = new BigDecimal(show_equation1.toString());
r = d1.add(d2);
}
if(d3!=null){
d4 = new BigDecimal(show_equation1.toString());
r = d3.subtract(d4);
}
if(d5!=null){
d6 = new BigDecimal(show_equation1.toString());
r = d5.multiply(d6);
}
if(d7!=null){
d8 = new BigDecimal(show_equation1.toString());
r = d7.divide(d8);
}
// show_equation1.append("=");
show_equation1.delete(0,show_equation1.length());
show_equation1.append(r);
tv.setText(show_equation1);
// Iterator it1 = calculate_equation1.iterator();
// while(it1.hasNext()){
// show_equation1.append(it1.next());
// }calculate_equation.clear();
//
// tv.setText(show_equation1);
// tv.setSelection(tv.getText().length());
}
});
point.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_equation1.append(".");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.b1:
show_equation1.append("1");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(1);break;
case R.id.b2:
show_equation1.append("2");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(2);break;
case R.id.b3:show_equation1.append("3");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(3);break;
case R.id.b4:show_equation1.append("4");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(4);break;
case R.id.b5:show_equation1.append("5");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(5);break;
case R.id.b6:show_equation1.append("6");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(6);break;
case R.id.b7:show_equation1.append("7");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(7);break;
case R.id.b8:show_equation1.append("8");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(8);break;
case R.id.b9:show_equation1.append("9");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(9);break;
case R.id.b0:show_equation1.append("0");
tv.setText(show_equation1);
tv.setSelection(tv.getText().length());
calculate_equation.add(0);break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".student05.Student05Activity06">
<EditText
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#000"
android:textSize="70sp" />
<Button
android:id="@+id/clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除"
android:textSize="50sp"
android:textColor="#000"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="445dp">
<TableRow>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="1"
android:textSize="50sp" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="2"
android:textSize="50sp" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="3"
android:textSize="50sp" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:text="+"
android:textSize="50sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="4"
android:textSize="50sp" />
<Button
android:id="@+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="5"
android:textSize="50sp" />
<Button
android:id="@+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="6"
android:textSize="50sp" />
<Button
android:id="@+id/sbc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:text="-"
android:textSize="50sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/b7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="7"
android:textSize="50sp" />
<Button
android:id="@+id/b8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="8"
android:textSize="50sp" />
<Button
android:id="@+id/b9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="9"
android:textSize="50sp" />
<Button
android:id="@+id/mla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:text="*"
android:textSize="50sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="."
android:textSize="50sp" />
<Button
android:id="@+id/b0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="0"
android:textSize="50sp" />
<Button
android:id="@+id/eq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="="
android:textSize="50sp" />
<Button
android:id="@+id/chu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:text="/"
android:textSize="50sp" />
</TableRow>
<Button
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="4"
android:text="返回主界面"
android:textSize="50sp" />
</TableLayout>
</LinearLayout>