.Java文件
未注释的部分是传统单显示窗口计算器,后面注释掉的函数也可以使用是双显示的计算器基本和安卓手机自带相似
package com.example.jisuanqi;
import static android.text.method.TextKeyListener.clear;
import static java.nio.file.Files.delete;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import java.math.BigDecimal;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String firstNum = "";
// 运算符
private String operator = "";
// 第二个操作数
private String secondNum = "";
// 当前的计算结果
private String result = "";
// 显示的文本内容
private String showText = "";
// 当前运算结果
private String resultsText = "";
TextView tv_input;
TextView tv_results;
ImageButton btn_del;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_input = findViewById(R.id.tv_input);
// tv_results = findViewById(R.id.tv_results);
ImageButton iv_back = (ImageButton) findViewById(R.id.iv_back);
iv_back.setOnClickListener(this);
Button btn_1 = (Button) findViewById(R.id.btn_1);
btn_1.setOnClickListener(this);
Button btn_2 = (Button) findViewById(R.id.btn_2);
btn_2.setOnClickListener(this);
Button btn_3 = (Button) findViewById(R.id.btn_3);
btn_3.setOnClickListener(this);
Button btn_4 = (Button) findViewById(R.id.btn_4);
btn_4.setOnClickListener(this);
Button btn_5 = (Button) findViewById(R.id.btn_5);
btn_5.setOnClickListener(this);
Button btn_6 = (Button) findViewById(R.id.btn_6);
btn_6.setOnClickListener(this);
Button btn_7 = (Button) findViewById(R.id.btn_7);
btn_7.setOnClickListener(this);
Button btn_8 = (Button) findViewById(R.id.btn_8);
btn_8.setOnClickListener(this);
Button btn_9 = (Button) findViewById(R.id.btn_9);
btn_9.setOnClickListener(this);
Button btn_0 = (Button) findViewById(R.id.btn_0);
btn_0.setOnClickListener(this);
Button btn_sub = (Button) findViewById(R.id.btn_sub);
btn_sub.setOnClickListener(this);
Button btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(this);
Button btn_eq = (Button) findViewById(R.id.btn_eq);//=
btn_eq.setOnClickListener(this);
Button btn_pt = (Button) findViewById(R.id.btn_pt);//.
btn_pt.setOnClickListener(this);
Button btn_clr = (Button) findViewById(R.id.btn_clr);//C
btn_clr.setOnClickListener(this);
Button btn_div = (Button) findViewById(R.id.btn_div);//除
btn_div.setOnClickListener(this);
Button btn_mul = (Button) findViewById(R.id.btn_mul);//*
btn_mul.setOnClickListener(this);
btn_del = findViewById(R.id.iv_del);//del
btn_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String inputText = "";
if (v.getId() != R.id.iv_del && v.getId() != R.id.iv_back){
inputText = ((TextView) v).getText().toString();
}
switch (v.getId()) {
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
Nums(inputText);
break;
case R.id.btn_add:
case R.id.btn_sub:
case R.id.btn_div:
case R.id.btn_mul:
Operators(inputText);
break;
case R.id.btn_eq:
Eq();
break;
case R.id.iv_del:
Delete();
break;
case R.id.iv_back:
finish();
break;
case R.id.btn_pt:
Points(inputText);
break;
case R.id.btn_clr:
clear();
break;
}
}
private void Nums(String Text) {
if (operator.length()==0){
firstNum+=Text;
showText=firstNum;
}else if (operator.length()!=0){
secondNum+=Text;
showText=secondNum;
}
Dis();
}
private void Operators(String op) {
if (firstNum.length()==0){
if (op.equals("-")){
firstNum=op;
showText=firstNum;
}
}else if (firstNum.length()!=0 && operator.length()==0){
if (firstNum.equals("-")){
if (op.equals("+")){
firstNum="";
showText=firstNum;
}
}else{
operator=op;
showText="";
}
}else if (operator.length()!=0 && secondNum.length()==0){
operator=op;
showText="";
}else if(operator.length()!=0 && secondNum.length()!=0){
result=count();
secondNum="";
firstNum=result;
operator=op;
showText="";
}
Dis();
}
private String count() {
String str="";
BigDecimal bd1 = new BigDecimal(firstNum);
BigDecimal bd2 = new BigDecimal(secondNum);
double d=0;
try {
switch (operator){
case "+":
d = bd1.add(bd2).doubleValue();
break;
case "-":
d = bd1.subtract(bd2).doubleValue();
break;
case "×":
d = bd1.multiply(bd2).setScale(15,BigDecimal.ROUND_HALF_UP).doubleValue();
break;
default:
if (bd2.doubleValue() == 0){
clear();
return "Error";
}
d = bd1.divide(bd2,20, BigDecimal.ROUND_HALF_UP).doubleValue();
break;
}
}catch (Exception e){
e.printStackTrace();
return "错误";
}
str=String.valueOf(d);
String str1 = str.substring(str.length() - 2);
if (str1.equals(".0"))
str = str.substring(0, str.length() - 2);
return str;
}
private void Eq() {
try {
if (firstNum.length()==0){
} else if (secondNum.length()==0) {
if (firstNum.equals("-")){
showText="0";
}else {
showText=String.valueOf(Double.valueOf(firstNum));
String str1 = showText.substring(showText.length() - 2);
if (str1.equals(".0"))
showText = showText.substring(0, showText.length() - 2);
}
}else{
showText=count();
}
Dis();
}catch (Exception e){
e.printStackTrace();
}
}
private void Delete() {
try {
if (firstNum.length()!=0 && operator.length()==0){
if (showText.equals(".") ||showText.equals("0.")){
firstNum="";
showText=firstNum;
}else {
firstNum=firstNum.substring(0,firstNum.length()-1);
showText=firstNum;
}
}else if(firstNum.length()!=0 && operator.length()!=0 && secondNum.length()==0){
operator="";
} else if (secondNum.length()!=0) {
secondNum=secondNum.substring(0,secondNum.length()-1);
showText=secondNum;
}
Dis();
}catch (Exception e){
e.printStackTrace();
}
}
private void Points(String inputText) {
if (firstNum.length()==0){
showText=".";
firstNum="0.";
} else if (firstNum.length()!=0 &&operator.length()==0) {
if (firstNum.equals("0.")){
}else if (firstNum.indexOf(".")==-1){
firstNum+=".";
showText=firstNum;
}
} else if (firstNum.length()!=0 && operator.length()!=0 && secondNum.length()==0) {
secondNum="0.";
showText+=".";
} else if (firstNum.length()!=0 && operator.length()!=0 && secondNum.length()!=0) {
secondNum+=".";
showText=firstNum;
}
Dis();
}
private void clear() {
firstNum="";
secondNum="";
showText="";
operator="";
result="";
Dis();
}
private void Dis(){
tv_input.setText(showText);
}
// private void clear() {
// resultsText = "";
// firstNum="";
// secondNum="";
// showText="";
// operator="";
// Dis();
// }
//
// private void Eq() {
// try {
// if (firstNum.length()==0){
// if (showText!="" && showText!=null){
// resultsText="0";
// }
// } else if (secondNum.length()==0) {
// if (firstNum.equals("-") || firstNum.equals("+")){
// resultsText="0";
// }else {
// resultsText=String.valueOf(Double.valueOf(firstNum));
// String str1 = resultsText.substring(resultsText.length() - 2);
// if (str1.equals(".0"))
// resultsText = resultsText.substring(0, resultsText.length() - 2);
// }
// }else{
// resultsText=count();
// }
// Dis();
// }catch (Exception e){
// e.printStackTrace();
// }
//
// }
//
// private String count() {
// String str="";
// BigDecimal bd1 = new BigDecimal(firstNum);
// BigDecimal bd2 = new BigDecimal(secondNum);
// double d=0;
// try {
// switch (operator){
// case "+":
// d = bd1.add(bd2).doubleValue();
// break;
// case "-":
// d = bd1.subtract(bd2).doubleValue();
// break;
// case "×":
// d = bd1.multiply(bd2).doubleValue();
// break;
// default:
// if (bd2.doubleValue() == 0)
// return "Error";
// d = bd1.divide(bd2).doubleValue();
// break;
// }
// }catch (Exception e){
// e.printStackTrace();
// return "错误";
// }
// str=String.valueOf(d);
// String str1 = str.substring(str.length() - 2);
// if (str1.equals(".0"))
// str = str.substring(0, str.length() - 2);
Dis();
// return str;
//
// }
//
// private void Nums(String Text) {
// if (operator.length()==0){
// firstNum+=Text;
// showText+=Text;
// }else if (operator.length()!=0){
// secondNum+=Text;
// showText+=Text;
// }
// Dis();
// }
//
// private void Points(String pt) {
// if (firstNum.length()==0){
// showText+=".";
// firstNum="0.";
// } else if (firstNum.length()!=0 &&operator.length()==0) {
// if (firstNum.equals("0.")){
// showText+=".";
// }else if (firstNum.indexOf(".")==-1){
// showText+=".";
// firstNum+=".";
// }else if (firstNum.substring(firstNum.length()-1).equals(".")){
// showText+=".";
// }
// } else if (firstNum.length()!=0 && operator.length()==0 && secondNum.length()==0) {
// secondNum="0.";
// showText+="0.";
// } else if (firstNum.length()!=0 && operator.length()!=0 && secondNum.length()!=0) {
// showText+=".";
// secondNum+=".";
// }
// Dis();
// }
//
// private void Operators(String op) {
// if (firstNum.length()==0){
// if (op.equals("-") ||op.equals("+")){
// firstNum=op;
// showText+=op;
// }else {
// showText+=op;
// }
// }else if (firstNum.length()!=0 && operator.length()==0){
// if (firstNum.equals("-") ||firstNum.equals("+")){
// if (op.equals("-") ||op.equals("+")){
// firstNum=op;
// showText+=op;
// }else {
// showText+=op;
// }
// }else {
// operator=op;
// showText+=operator;
// }
// }else if (operator.length()!=0 && secondNum.length()==0){
// operator=op;
// showText+=operator;
// }else if(operator.length()!=0 && secondNum.length()!=0){
// result=count();
// secondNum="";
// firstNum=result;
// operator=op;
// showText+=op;
// }
// Dis();
// }
//
// private void Delete(){
// try {
// if (showText.length()!=0 && firstNum.length()==0){
// showText=showText.substring(0,showText.length()-1);
// }else if(showText.length()!=0 && firstNum.length()!=0 && operator.length()==0){
// firstNum=firstNum.substring(0,firstNum.length()-1);
// showText=showText.substring(0,showText.length()-1);
// } else if (operator.length()!=0 && secondNum.length()==0) {
// operator="";
// showText=showText.substring(0,showText.length()-1);
// } else if (secondNum.length()!=0) {
// secondNum=secondNum.substring(0,secondNum.length()-1);
// showText=showText.substring(0,showText.length()-1);
// }
// Dis();
// }catch (Exception e){
// e.printStackTrace();
// }
// }
// private void Dis(){
// tv_input.setText(showText);
// tv_results.setText(resultsText);
// }
}
layout
布局文件可以自己手拉设计一下,只要id对上号就可以了
activity_main.xml
线性布局的计算器
<?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:background="#ffe9ecf1"
android:orientation="vertical"><RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffffffff"><ImageButton
android:id="@+id/iv_back"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:scaleType="fitCenter"
android:src="@xml/ic_back"
tools:ignore="SpeakableTextPresentCheck"
/><TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#ff000000"
android:textSize="17sp"
android:text="简易计算器" /></RelativeLayout>
<TextView
android:id="@+id/tv_input"
android:layout_width="match_parent"
android:layout_height="158dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:gravity="right"
android:padding="5dp"
android:textColor="#ff000000"
android:textSize="50sp"
tools:text="520-520" /><TextView
android:id="@+id/tv_results"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="right"
android:padding="5dp"
android:textColor="#FF959595"
android:textSize="40sp"
tools:text="0" /><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@xml/shape_module"
android:orientation="vertical"
android:padding="20dp"><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"><Button
android:id="@+id/btn_clr"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:radius="100dp"
android:shadowRadius="100"
android:text="C"
android:textColor="#FF0080FF"
android:textSize="35sp" /><Button
android:id="@+id/btn_div"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="÷"
android:textColor="#FF0080FF"
android:textSize="35sp" /><Button
android:id="@+id/btn_mul"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="×"
android:textColor="#FF0080FF"
android:textSize="35sp" /><ImageButton
android:id="@+id/iv_del"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
tools:ignore="SpeakableTextPresentCheck"
android:src="@xml/ic_delete"/></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="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="7"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="8"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="9"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_sub"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="-"
android:textColor="#FF0080FF"
android:textSize="35sp" /></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="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="4"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="5"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="6"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="+"
android:textColor="#FF0080FF"
android:textSize="35sp" /></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"><LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical"><LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"><Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="1"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="2"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="3"
android:textColor="#ff000000"
android:textSize="35sp" /></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"><Button
android:id="@+id/btn_0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="2"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="0"
android:textColor="#ff000000"
android:textSize="35sp" /><Button
android:id="@+id/btn_pt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@xml/radio_button_selector"
android:padding="10dp"
android:text="."
android:textColor="#ff000000"
android:textSize="35sp" /></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"><Button
android:id="@+id/btn_eq"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:background="@xml/shape_radio_button_unpressed"
android:backgroundTint="#FF0080FF"
android:padding="10dp"
android:text="="
android:textColor="#ffffffff"
android:textSize="35sp" /></LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
.xml文件
若要使用该布局需要在src里创建xml文件夹将下方文件导入使用,注意文件名一一对应
data_extraction_rules.xml
<?xml version="1.0" encoding="utf-8"?><!--
Sample data extraction rules file; uncomment and customize as necessary.
See https://developer.android.com/about/versions/12/backup-restore#xml-changes
for details.
-->
<data-extraction-rules>
<cloud-backup>
<!-- TODO: Use <include> and <exclude> to control what is backed up.
<include .../>
<exclude .../>
-->
</cloud-backup>
<!--
<device-transfer>
<include .../>
<exclude .../>
</device-transfer>
-->
</data-extraction-rules>
backup_rules.xml
<?xml version="1.0" encoding="utf-8"?><!--
Sample backup rules file; uncomment and customize as necessary.
See https://developer.android.com/guide/topics/data/autobackup
for details.
Note: This file is ignored for devices older that API 31
See https://developer.android.com/about/versions/12/backup-restore
-->
<full-backup-content>
<!--
<include domain="sharedpref" path="."/>
<exclude domain="sharedpref" path="device.xml"/>
-->
</full-backup-content>
ic_back.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/black"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M11.67,3.87L9.9,2.1 0,12l9.9,9.9 1.77,-1.77L3.54,12z" />
</vector>
radio_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@xml/shape_radio_button_pressed" android:state_pressed="true" />
<item android:drawable="@xml/shape_radio_button_unpressed" /></selector>
shape_radio_button_unp
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 指定了形状内部的填充颜色 -->
<solid android:color="#E6ECEC" /><!-- 指定了形状轮廓的粗细与颜色 -->
<stroke
android:width="1dp"
android:color="#8AB0BB" /><!-- 指定了形状四个圆角的半径 -->
<corners android:radius="100dp" /></shape>
If_delete.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="30dp"
android:height="30dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#FF0080FF">
<path
android:fillColor="@android:color/white"
android:pathData="M22,3L7,3c-0.69,0 -1.23,0.35 -1.59,0.88L0,12l5.41,8.11c0.36,0.53 0.9,0.89 1.59,0.89h15c1.1,0 2,-0.9 2,-2L24,5c0,-1.1 -0.9,-2 -2,-2zM19,15.59L17.59,17 14,13.41 10.41,17 9,15.59 12.59,12 9,8.41 10.41,7 14,10.59 17.59,7 19,8.41 15.41,12 19,15.59z"/>
</vector>
Shape_module.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 指定了形状内部的填充颜色 -->
<solid android:color="#FFF4F4F4" /><!-- 指定了形状四个圆角的半径 -->
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp" /></shape>
简易计算机供初学者参考,交作业使用