一、基本算術運算功能
計算器是我們日常生活中非常常見的工具之一,而作為一個開發者,能夠開發出自己的計算器也是一種很具有成就感的事情。本文將介紹如何使用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/zh-hant/n/183045.html