一、基本算术运算功能
计算器是我们日常生活中非常常见的工具之一,而作为一个开发者,能够开发出自己的计算器也是一种很具有成就感的事情。本文将介绍如何使用Android Studio开发一个基本算术运算的计算器。
在进行开发之前,我们需要了解计算器需要具备哪些基本的功能。首先,它需要支持常见的加、减、乘、除运算。我们可以使用Java语言中的计算方法来实现,例如:
int add(int a, int b) {//加法运算
return a + b;
}
int substract(int a, int b) {//减法运算
return a - b;
}
int multiply(int a, int b) {//乘法运算
return a * b;
}
int divide(int a, int b) {//除法运算
if (b == 0) {//除数不能为0
throw new IllegalArgumentException("除数不能为0");
}
return a / b;
}
然后,我们需要在界面上添加数字按键和运算符按键,并且需要将它们的点击事件与计算器的输入和计算相关联。接下来,我们可以通过以下步骤来实现一个简单的计算器:
1. 创建一个Android项目,并在布局文件中添加一个EditText控件用于显示计算结果和输入数字,再添加多个Button控件用于输入数字和运算符。
<RelativeLayout 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"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:inputType="number"
android:textAlignment="center"
android:textSize="30sp"/>
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="1"
android:textSize="30sp"
android:layout_below="@+id/et_input"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="2"
android:textSize="30sp"
android:layout_below="@+id/et_input"
android:layout_toRightOf="@+id/button1"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button3"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="3"
android:textSize="30sp"
android:layout_below="@+id/et_input"
android:layout_toRightOf="@+id/button2"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button4"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="+"
android:textSize="30sp"
android:layout_below="@+id/et_input"
android:layout_toRightOf="@+id/button3"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button5"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="4"
android:textSize="30sp"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button6"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="5"
android:textSize="30sp"
android:layout_below="@+id/button1"
android:layout_toRightOf="@+id/button5"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button7"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="6"
android:textSize="30sp"
android:layout_below="@+id/button1"
android:layout_toRightOf="@+id/button6"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button8"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="-"
android:textSize="30sp"
android:layout_below="@+id/button1"
android:layout_toRightOf="@+id/button7"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button9"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="7"
android:textSize="30sp"
android:layout_below="@+id/button5"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button10"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="8"
android:textSize="30sp"
android:layout_below="@+id/button5"
android:layout_toRightOf="@+id/button9"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button11"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="9"
android:textSize="30sp"
android:layout_below="@+id/button5"
android:layout_toRightOf="@+id/button10"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button12"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="*"
android:textSize="30sp"
android:layout_below="@+id/button5"
android:layout_toRightOf="@+id/button11"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button13"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="."
android:textSize="30sp"
android:layout_below="@+id/button9"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button14"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="0"
android:textSize="30sp"
android:layout_below="@+id/button9"
android:layout_toRightOf="@+id/button13"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button15"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="="
android:textSize="30sp"
android:layout_below="@+id/button9"
android:layout_toRightOf="@+id/button14"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button16"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="/"
android:textSize="30sp"
android:layout_below="@+id/button9"
android:layout_toRightOf="@+id/button15"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
2. 在MainActivity类中定义一个变量用于保存当前输入的数字,定义一个变量用于保存当前输入的运算符,定义一个布尔型变量用于记录当前是否计算过表达式。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//输入的数字
private String mText = "";
//输入的运算符
private String mOperator = "";
//是否计算过表达式
private boolean mIsCalculated = false;
//...
}
3. 在MainActivity类的onCreate方法中找到布局中的所有数字和运算符按钮,并为它们设置点击事件。在点击事件中,根据用户输入的数字和运算符进行操作,最后将结果显示在EditText中。
public class MainActivity extends AppCompatActivity implements View.OnClickListener { //...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button button10 = (Button) findViewById(R.id.button10);
Button button11 = (Button) findViewById(R.id.button11);
Button button12 = (Button) findViewById(R.id.button12);
Button button13 = (Button) findViewById(R.id.button13);
Button button14 = (Button) findViewById(R.id.button14);
Button button15 = (Button) findViewById(R.id.button15);
Button button16 = (Button) findViewById(R.id.button16);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button10.setOnClickListener(this);
button11.setOnClickListener(this);
button12.setOnClickListener(this);
button13.setOnClickListener(this);
button14.setOnClickListener(this);
button15.setOnClickListener(this);
button16.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mIsCalculated) {
mText = "";
mOperator = "";
mIsCalculated = false;
}
switch (view.getId()) {
case R.id.button1:
clickNumber("1");
break;
case R.id.button2:
clickNumber("2");
break;
case R.id.button3:
clickNumber("3");
break;
case R.id.button5:
clickNumber("4");
break;
case R.id.button6:
clickNumber("5");
break;
case R.id.button7:
clickNumber("6");
break;
case R.id.button9:
clickNumber("7");
break;
case R.id.button10:
clickNumber("8");
break;
case R.id.button11:
clickNumber("9");
break;
case R.id.button13:
clickNumber(".");
break;
case R.id.button14:
clickNumber("0");
break;
case R.id.button4:
clickOperator("+");
break;
case R.id.button8:
clickOperator("-");
break;
case R.id.button12:
clickOperator("*");
break;
case R.id.button16:
clickOperator("/");
break;
case R.id.button15:
clickEqual();
break;
}
}
//将数字添加到输入框中
private void clickNumber(String number) {
mText += number;
EditText editText = (EditText) findViewById(R.id.et_input);
editText.setText(mText);
}
//处理点击运算符时的逻辑
private void clickOperator(String operator) {
if (mText != "") {
mOperator = operator;
mText += operator;
EditText editText = (EditText) findViewById(R.id.et_input);
editText.setText(mText);
}
}
//处理点击“=”时的逻辑
private void clickEqual() {
if (mOperator != "" && mText != "") {
double result = 0;
String[] arr = mText.split("\\" + mOperator);
if (arr.length == 2) {
double num1 = Double.parseDouble(arr[0]);
double num2
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/183045.html
微信扫一扫
支付宝扫一扫