开发和测试应用程序
发布时间:2025-06-24 17:35:53 作者:北方职教升学中心 阅读量:467
创建一个新的项目
完成上面步骤以后,点击Finish,等待加载好项目就可以继续下面的步骤了
二、计算器是一类被广泛使用的应用程序之一,因此学习如何开发一款简易的计算器应用程序是学习Android Studio开发的一个很好的开始。运算效果
一、创建一个新的项目
二、实现Java运算逻辑
编辑
完整代码
xml文件代码:
Java文件代码:
注:
前言
随着移动互联网的普及,手机应用程序已经成为人们生活中不可或缺的一部分。接下来我将为大家介绍如何使用Android Studio创建一个简易的计算器应用程序。开发和测试应用程序。
xml文件代码附到最后啦,根据自己需要自行截取复制。实现Java运算逻辑
写好基本的显示页面后,咱就得来完成逻辑运算和点击事件了
点开MainActivity.java文件:
Java逻辑要完成的主要是如何设计点击事件以及如何实现加减乘除的逻辑
完整代码
xml文件代码:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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" app:circularflow_angles="20" tools:context=".MainActivity"><!--csdn 波士顿o泡果奶 版权所有 --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraintLayout" android:layout_width="match_parent" android:layout_height="80dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/imageView" android:layout_width="29dp" android:layout_height="25dp" android:layout_marginTop="18dp" android:layout_marginEnd="10dp" app:layout_constraintEnd_toStartOf="@+id/imageView2" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/img" /> <ImageView android:id="@+id/imageView2" android:layout_width="32dp" android:layout_height="29dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/img_1" /> <TextView android:id="@+id/textView" android:layout_width="65dp" android:layout_height="41dp" android:layout_marginStart="16dp" android:layout_marginTop="7dp" android:text="计算" android:textColor="#010101" android:textSize="31dp" android:textStyle="bold" app:autoSizeTextType="none" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="9dp" android:layout_marginTop="14dp" android:text="汇率" android:textColor="#B2B2B2" android:textSize="25dp" android:textStyle="bold" app:layout_constraintStart_toEndOf="@+id/textView" app:layout_constraintTop_toTopOf="parent" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="57dp" android:layout_height="3dp" android:layout_marginStart="19dp" android:background="#FF0101" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView"> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraintLayout2" android:layout_width="410dp" android:layout_height="178dp" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/constraintLayout"> <EditText android:id="@+id/editTextText2" android:layout_width="409dp" android:layout_height="83dp" android:layout_marginStart="2dp" android:layout_marginTop="76dp" android:ems="10" android:inputType="text" android:textSize="50sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="466dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/constraintLayout2"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraintLayout51" android:layout_width="409dp" android:layout_height="89.8dp" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.cardview.widget.CardView android:id="@+id/cardView2" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/AC" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="18dp" android:layout_marginTop="16dp" android:text="AC" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:id="@+id/cardView" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView3" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView2" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/dl" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView3" android:layout_width="48dp" android:layout_height="30dp" android:layout_marginStart="14dp" android:layout_marginTop="28dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/img_4" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:id="@+id/cardView3" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView4" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:text="+/-" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:id="@+id/cardView4" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F6E9E8" app:cardCornerRadius="45dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView3" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/D" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="10dp" android:text="÷ " android:textSize="42sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraintLayout54" android:layout_width="409dp" android:layout_height="89.8dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/constraintLayout51"> <androidx.cardview.widget.CardView android:id="@+id/cardView2" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/seven" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="17dp" android:text="7" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView3" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView2" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/eight" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="17dp" android:text="8" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView3" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView4" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/nine" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="17dp" android:text="9" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView4" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F6E9E8" app:cardCornerRadius="45dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView3" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/X" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView19" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="31dp" android:layout_marginTop="14dp" android:text="x" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraintLayout52" android:layout_width="409dp" android:layout_height="89.8dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/constraintLayout54"> <androidx.cardview.widget.CardView android:id="@+id/cardView2" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/four" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView20" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="16dp" android:text="4" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView3" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView2" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/five" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView21" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="16dp" android:text="5" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView3" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView4" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/six" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView22" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="16dp" android:text="6" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView4" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F6E9E8" app:cardBackgroundColor="#F6E9E8" app:cardCornerRadius="45dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView3" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/sub" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView23" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="35dp" android:layout_marginTop="16dp" android:text="-" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraintLayout53" android:layout_width="409dp" android:layout_height="89.8dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/constraintLayout52"> <androidx.cardview.widget.CardView android:id="@+id/cardView2" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/one" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView24" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="16dp" android:text="1" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView3" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView2" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/two" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView25" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="16dp" android:text="2" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView3" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView4" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/three" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView26" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="16dp" android:text="3" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView4" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F6E9E8" app:cardCornerRadius="45dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView3" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/add" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView27" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="16dp" android:text="+" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="409dp" android:layout_height="89.8dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/constraintLayout53"> <androidx.cardview.widget.CardView android:id="@+id/cardView2" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/yu" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView28" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="27dp" android:layout_marginTop="17dp" android:text="%" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView3" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView2" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/zero" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView29" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginTop="17dp" android:text="0" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView3" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F2F1F1" app:cardCornerRadius="45dp" app:layout_constraintEnd_toStartOf="@+id/cardView4" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/point" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView30" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="36dp" android:layout_marginTop="17dp" android:text="." android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.cardview.widget.CardView android:id="@+id/cardView4" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="5dp" android:background="#F2F1F1" app:cardBackgroundColor="#F85955" app:cardCornerRadius="45dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/cardView3" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/equal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView32" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="31dp" android:layout_marginTop="16dp" android:text="=" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout></androidx.constraintlayout.widget.ConstraintLayout>
Java文件代码:
import androidx.appcompat.app.AppCompatActivity;import androidx.constraintlayout.widget.ConstraintLayout;import android.os.Bundle;import android.view.View;import android.widget.EditText;import java.math.BigDecimal;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private StringBuilder show=new StringBuilder(); private ArrayList calculate_equation; private int signal=0;//为0 时表示刚输入状态;为1时表示当前在输出结果上继续输入 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show=new StringBuilder(); calculate_equation=new ArrayList<>();//李木 ConstraintLayout AC = findViewById(R.id.AC); ConstraintLayout Divide = findViewById(R.id.D); ConstraintLayout Multiplication = findViewById(R.id.X); ConstraintLayout Sub = findViewById(R.id.sub); ConstraintLayout Add = findViewById(R.id.add); ConstraintLayout Equal = findViewById(R.id.equal); ConstraintLayout Point = findViewById(R.id.point); ConstraintLayout one = findViewById(R.id.one); ConstraintLayout two = findViewById(R.id.two); ConstraintLayout three = findViewById(R.id.three); ConstraintLayout four = findViewById(R.id.four); ConstraintLayout five = findViewById(R.id.five); ConstraintLayout six = findViewById(R.id.six); ConstraintLayout seven = findViewById(R.id.seven); ConstraintLayout eight = findViewById(R.id.eight); ConstraintLayout nine = findViewById(R.id.nine); ConstraintLayout zero = findViewById(R.id.zero); EditText result=findViewById(R.id.editTextText2); AC.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { show.delete(0,show.length()); show.append(""); result.setText(show); result.setSelection(result.getText().length()); signal=0; } }); zero.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ if(!(show.toString().equals("0"))){ if(signal==0){ show.append("0"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("0"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } } }); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("1"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("1"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("2"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("2"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("3"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("3"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); four.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("4"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("4"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); five.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("5"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("5"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); six.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("6"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("6"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); seven.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("7"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("7"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); eight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("8"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("8"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); nine.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show.append("9"); result.setText(show); result.setSelection(result.getText().length()); }else{ show.delete(0,show.length()); show.append("9"); result.setText(show); result.setSelection(result.getText().length()); signal=0; } } }); Point.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ String a=show.toString(); if(a.equals("")){ show.append("."); result.setText(show); result.setSelection(result.getText().length()); }else{ int i; char t='0'; for(i=a.length();i>0;i--){ t=a.charAt(i-1); if(t=='.'||t=='+'||t=='-'||t=='*'||t=='/') break; } if(i==0){ show.append("."); result.setText(show); result.setSelection(result.getText().length()); }else if(t=='+'||t=='-'||t=='*'||t=='/'){ show.append("."); result.setText(show); result.setSelection(result.getText().length()); } } }else{ show.delete(0,show.length()); show.append("."); result.setText("."); result.setSelection(result.getText().length()); signal=0; } } }); Equal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判断用户是否输入了内容 if(!show.toString().equals("")){ signal=1; char temp=show.charAt(show.length()-1); if(show.charAt(0)=='-') show.insert(0,"0"); if(temp=='+'||temp=='-') show.append("0"); if(temp=='*'||temp=='/') show.append("1"); StringBuilder temp1=new StringBuilder(); for(int i=0;i<show.length();i++){ if(show.charAt(i)>='0'&&show.charAt(i)<='9'||show.charAt(i)=='.'){ temp1.append(String.valueOf(show.charAt(i))); }else if(show.charAt(i)=='N'){ calculate_equation.add("NaN"); i=i+2; }else if(show.charAt(i)=='∞'){ calculate_equation.add("∞"); } else { if(temp1.length()!=0){ calculate_equation.add(temp1.toString()); temp1.delete(0,temp1.length()); } calculate_equation.add(String.valueOf(show.charAt(i))); } } if(temp1.length()!=0){ calculate_equation.add(temp1.toString()); } calculate_equation.add("#"); String temp8=calculate(calculate_equation); result.setText(temp8); result.setSelection(result.getText().length()); show.delete(0,show.length()); calculate_equation.clear(); show.append(temp8); } } }); Add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判断用户是否输入了内容 if(!(show.toString().equals(""))) { signal=0; char temp=show.charAt(show.length()-1); if(temp=='+'||temp=='-'||temp=='*'||temp=='/') { show.deleteCharAt(show.length()-1); show.append("+"); } else show.append("+"); result.setText(show); result.setSelection(result.getText().length()); } } }); Sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判断用户是否输入了内容 if(!(show.toString().equals(""))) { signal=0; char temp=show.charAt(show.length()-1); if(temp=='+'||temp=='-'||temp=='*'||temp=='/') { show.deleteCharAt(show.length()-1); show.append("-"); } else show.append("-"); result.setText(show); result.setSelection(result.getText().length()); } } }); Multiplication.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判断用户是否输入了内容 if(!(show.toString().equals(""))) { signal=0; char temp=show.charAt(show.length()-1); if(temp=='+'||temp=='-'||temp=='*'||temp=='/') { show.deleteCharAt(show.length()-1); show.append("*"); } else show.append("*"); result.setText(show); result.setSelection(result.getText().length()); } } }); Divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判断用户是否输入了内容 if(!(show.toString().equals(""))) { signal=0; char temp=show.charAt(show.length()-1); if(temp=='+'||temp=='-'||temp=='*'||temp=='/') { show.deleteCharAt(show.length()-1); show.append("/"); } else show.append("/"); result.setText(show); result.setSelection(result.getText().length()); } } }); } protected boolean operatorPriorityCompare(char operator1,char operator2) { int o1=0; int o2=0; switch (operator1){ case '+':{o1=0;break;} case '-':{o1=0;break;} case '*':{o1=1;break;} case '/':{o1=1;break;} } switch (operator2){ case '+':{o2=0;break;} case '-':{o2=0;break;} case '*':{o2=1;break;} case '/':{o2=1;break;} } if(o1<=o2) { return false; } else return true; } //相加 public static Double Add(Double d1,Double d2) { if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){ return d1+d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1+d2; } BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.add(b2).doubleValue(); } //相减 public static Double Sub(Double d1,Double d2){ if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){ return d1-d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1-d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1*d2; } BigDecimal b1=new BigDecimal(Double.toString(d1)); BigDecimal b2=new BigDecimal(Double.toString(d2)); return b1.subtract(b2).doubleValue(); } //相乘 public static Double Mul(Double d1,Double d2){ if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){ return d1*d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1*d2; } BigDecimal b1=new BigDecimal(Double.toString(d1)); BigDecimal b2=new BigDecimal(Double.toString(d2)); return b1.multiply(b2).setScale(8).doubleValue(); } //相除 public static Double Div(Double d1,Double d2){ if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){ return d1/d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1/d2; } if(d1==0.0&&d2==0.0){ return Double.NaN; } if(d2==0.0){ return d1/d2; } BigDecimal b1=new BigDecimal(Double.toString(d1)); BigDecimal b2=new BigDecimal(Double.toString(d2)); return b1.divide(b2,8,BigDecimal.ROUND_HALF_UP).doubleValue(); } protected String calculate(ArrayList equation){ Double temp2; Double temp3; Double result; List operator=new ArrayList(); List<Double> operand=new ArrayList(); for(int i=0;i<equation.size();i++) { String temp4=(String) equation.get(i); if(temp4.equals("+")||temp4.equals("-")||temp4.equals("*")||temp4.equals("/")) { if(operator.size()>0) { String temp5=operator.get(operator.size()-1).toString(); while(!(operatorPriorityCompare(temp4.charAt(0),temp5.charAt(0)))&&operator.size()>0) { operator.remove(operator.size()-1); temp3=operand.get(operand.size()-1); operand.remove(operand.size()-1); temp2=operand.get(operand.size()-1); operand.remove(operand.size()-1); switch (temp5.charAt(0)){ case '+':{result=Add(temp2,temp3);operand.add(result);break;} case '-':{result=Sub(temp2,temp3);operand.add(result);break;} case '*':{result=Mul(temp2,temp3);operand.add(result);break;} case '/':{result=Div(temp2,temp3);operand.add(result);break;} } if(operator.size()>0) { temp5=operator.get(operator.size()-1).toString(); } else break; } operator.add(temp4); } else operator.add(temp4); } else if(temp4.equals("#")) { while(operator.size()>0) { String temp6=(String)operator.get(operator.size()-1); operator.remove(operator.size()-1); temp3=operand.get(operand.size()-1); operand.remove(operand.size()-1); temp2=operand.get(operand.size()-1); operand.remove(operand.size()-1); switch (temp6.charAt(0)){ case '+':{result=Add(temp2,temp3);operand.add(result);break;} case '-':{result=Sub(temp2,temp3);operand.add(result);break;} case '*':{result=Mul(temp2,temp3);operand.add(result);break;} case '/':{result=Div(temp2,temp3);operand.add(result);break;} } } } else { if(temp4.equals("NaN")){ operand.add(Double.NaN); }else if(temp4.equals("∞")){ operand.add(Double.POSITIVE_INFINITY); }else{ operand.add(Double.parseDouble(temp4)); } } } if(operand.get(0)==Double.NEGATIVE_INFINITY) return "-∞"; if(operand.get(0)==Double.POSITIVE_INFINITY) return "∞"; return operand.get(0).toString(); }}/** csdn 波士顿o泡果奶 版权所有 */
注:
xml文件中有一些图片,若完全粘贴xml代码,可将图片换成自己的图片,图片资源放到哪里,这里我就不多说了,可以参考下面的那些博客安卓studio图片资源放到哪里- CSDN搜索https://so.csdn.net/so/search?q=%E5%AE%89%E5%8D%93studio%E5%9B%BE%E7%89%87%E8%B5%84%E6%BA%90%E6%94%BE%E5%88%B0%E5%93%AA%E9%87%8C&t=&u=&urw=