帮我用android studio写一个简单计算器,并用SQLite实现历史查询,使用java语言
时间: 2024-03-08 13:51:05 浏览: 82
使用android studio完成计算器的布局并实现其简单功能.zip
5星 · 资源好评率100%
好的,下面是实现步骤:
1. 新建一个Android Studio项目,命名为Calculator,选择Empty Activity作为模板
2. 在res/layout文件夹下,新建一个xml布局文件,命名为activity_main.xml,用于显示计算器界面,代码如下:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_marginBottom="16dp"
android:hint="0"
android:textSize="30sp"
android:padding="16dp"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5"
android:padding="16dp">
<Button
android:id="@+id/button7"
android:text="7"
android:layout_column="0"
android:layout_row="1"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button8"
android:text="8"
android:layout_column="1"
android:layout_row="1"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button9"
android:text="9"
android:layout_column="2"
android:layout_row="1"
android:layout_gravity="fill"/>
<Button
android:id="@+id/buttonDivide"
android:text="/"
android:layout_column="3"
android:layout_row="1"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button4"
android:text="4"
android:layout_column="0"
android:layout_row="2"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button5"
android:text="5"
android:layout_column="1"
android:layout_row="2"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button6"
android:text="6"
android:layout_column="2"
android:layout_row="2"
android:layout_gravity="fill"/>
<Button
android:id="@+id/buttonMultiply"
android:text="*"
android:layout_column="3"
android:layout_row="2"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button1"
android:text="1"
android:layout_column="0"
android:layout_row="3"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button2"
android:text="2"
android:layout_column="1"
android:layout_row="3"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button3"
android:text="3"
android:layout_column="2"
android:layout_row="3"
android:layout_gravity="fill"/>
<Button
android:id="@+id/buttonMinus"
android:text="-"
android:layout_column="3"
android:layout_row="3"
android:layout_gravity="fill"/>
<Button
android:id="@+id/button0"
android:text="0"
android:layout_column="0"
android:layout_row="4"
android:layout_gravity="fill"/>
<Button
android:id="@+id/buttonDot"
android:text="."
android:layout_column="1"
android:layout_row="4"
android:layout_gravity="fill"/>
<Button
android:id="@+id/buttonEquals"
android:text="="
android:layout_column="2"
android:layout_row="4"
android:layout_gravity="fill"/>
<Button
android:id="@+id/buttonPlus"
android:text="+"
android:layout_column="3"
android:layout_row="4"
android:layout_gravity="fill"/>
</GridLayout>
</LinearLayout>
```
3. 在MainActivity.java中实现计算器逻辑和历史记录功能,代码如下:
```
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private Button buttonDot, buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEquals;
private DBHelper dbHelper;
private double num1 = 0, num2 = 0;
private String operator = "";
private boolean isOperatorClicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
button0 = findViewById(R.id.button0);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button5 = findViewById(R.id.button5);
button6 = findViewById(R.id.button6);
button7 = findViewById(R.id.button7);
button8 = findViewById(R.id.button8);
button9 = findViewById(R.id.button9);
buttonDot = findViewById(R.id.buttonDot);
buttonPlus = findViewById(R.id.buttonPlus);
buttonMinus = findViewById(R.id.buttonMinus);
buttonMultiply = findViewById(R.id.buttonMultiply);
buttonDivide = findViewById(R.id.buttonDivide);
buttonEquals = findViewById(R.id.buttonEquals);
dbHelper = new DBHelper(this, "Calculator.db", null, 1);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "9");
}
});
buttonDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editText.getText().toString().contains(".")) {
editText.setText(editText.getText() + ".");
}
}
});
buttonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editText.getText().toString().isEmpty()) {
if (!isOperatorClicked) {
num1 = Double.parseDouble(editText.getText().toString());
operator = "+";
editText.setText("");
isOperatorClicked = true;
} else {
Toast.makeText(MainActivity.this, "请先完成上一个运算", Toast.LENGTH_SHORT).show();
}
}
}
});
buttonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editText.getText().toString().isEmpty()) {
if (!isOperatorClicked) {
num1 = Double.parseDouble(editText.getText().toString());
operator = "-";
editText.setText("");
isOperatorClicked = true;
} else {
Toast.makeText(MainActivity.this, "请先完成上一个运算", Toast.LENGTH_SHORT).show();
}
}
}
});
buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editText.getText().toString().isEmpty()) {
if (!isOperatorClicked) {
num1 = Double.parseDouble(editText.getText().toString());
operator = "*";
editText.setText("");
isOperatorClicked = true;
} else {
Toast.makeText(MainActivity.this, "请先完成上一个运算", Toast.LENGTH_SHORT).show();
}
}
}
});
buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editText.getText().toString().isEmpty()) {
if (!isOperatorClicked) {
num1 = Double.parseDouble(editText.getText().toString());
operator = "/";
editText.setText("");
isOperatorClicked = true;
} else {
Toast.makeText(MainActivity.this, "请先完成上一个运算", Toast.LENGTH_SHORT).show();
}
}
}
});
buttonEquals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editText.getText().toString().isEmpty()) {
if (isOperatorClicked) {
num2 = Double.parseDouble(editText.getText().toString());
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0) {
Toast.makeText(MainActivity.this, "除数不能为0", Toast.LENGTH_SHORT).show();
return;
}
result = num1 / num2;
break;
}
editText.setText(String.valueOf(result));
isOperatorClicked = false;
// 将计算结果保存到历史记录中
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("expression", num1 + operator + num2 + "=" + result);
db.insert("History", null, values);
values.clear();
}
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
// 查询历史记录
public void queryHistory(View view) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("History", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
String expression = cursor.getString(cursor.getColumnIndex("expression"));
Toast.makeText(MainActivity.this, expression, Toast.LENGTH_SHORT).show();
} while (cursor.moveToNext());
}
cursor.close();
}
}
```
4. 新建一个Java类,命名为DBHelper,用于创建和管理SQLite数据库,代码如下:
```
package com.example.calculator;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final String CREATE_HISTORY = "create table History ("
+ "id integer primary key autoincrement, "
+ "expression text)";
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_HISTORY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
```
5. 在AndroidManifest.xml中添加读写数据库的权限:
```
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
```
6. 运行程序,即可使用计算器,并且可以在点击“历史记录”按钮后查看历史计算结果。
阅读全文