该Demo涉及到的知识点:SQLiteOpenHelper, ListView, AlertDialog,SharedPreferences等的使用,代码比较简单直接贴上:
1)主界面的两个Button 入口
package com.example.qz;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Intent intent;
private Button btn1;
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init() {
btn1 = findViewById(R.id.button);
btn2 = findViewById(R.id.button2);
intent = new Intent();
}
//todo:记账点击事件
public void btn1(View view) {
intent.setClass(this,Qz.class);
startActivity(intent);
}
//todo:流水点击事件
public void btn2(View view) {
intent.setClass(this,De.class);
startActivity(intent);
}
}
对应的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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="314dp"
android:text="记账"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="btn1"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="流水"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/button"
android:onClick="btn2"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
2)记账功能的Java代码为:
package com.example.qz;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Qz extends AppCompatActivity {
private Button clearbtn;
private AlertDialog alertDialog;
//类别
private EditText editText;
//金额
private EditText editText1;
//日期
private EditText editText2;
//说明
private EditText editText3;
private SqlLiteHelper sqlLiteHelper;
private SharedPreferences sharedPreferences;
private SharedPreferences commitSharePreference;
private Context mContext;
private final String items[] = {"食物", "衣服", "娱乐", "租金", "交通", "医疗", "其他"};
private String qc_info = "qc_info";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qz2);
init();
}
public void init() {
clearbtn = findViewById(R.id.clearbtn);
editText = findViewById(R.id.edit_text);
sharedPreferences = getSharedPreferences("qz_value", MODE_PRIVATE);
commitSharePreference = getSharedPreferences("commit", MODE_PRIVATE);
editText.setText(sharedPreferences.getString("items", ""));
editText.setFocusable(false);
//金额,监听用户输入完成
editText1 = findViewById(R.id.edit_text2);
editText1.setInputType(EditorInfo.TYPE_CLASS_PHONE);
//日期
editText2 = findViewById(R.id.edit_text3);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date date = new Date(System.currentTimeMillis());
editText2.setText(simpleDateFormat.format(date));
editText2.setFocusable(false);
editText2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(Qz.this, "默认日期,无需修改!", Toast.LENGTH_SHORT).show();
return false;
}
});
//说明
editText3 = findViewById(R.id.edit_text4);
//创建数据库的对象
sqlLiteHelper = new SqlLiteHelper(Qz.this, "qz.db3", null, 1);
}