目前APP还存在很多问题,只实现了最基本的添加,查看,删除功能,
如有机会以后打算再进行功能的扩充。
涉及哪些知识:
页面的基本布局、ListView的使用、SQLite数据库的基本操作、Dialog对话框、按钮的点击事件、单选框的使用、自动获取系统时间等
主要代码
package com.JIN; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import androidx.annotation.Nullable; public class SQL extends SQLiteOpenHelper { final String add="create table jizhangben (money varchar(20),time varchar(20),driction varchar(10),info varchar(50))"; public SQL(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, null, version); } @Override
package com.JIN; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.GestureDetector; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private long exitTime=0; private SQL sqL; private SQLiteDatabase sqliteDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); makeview(); } public void makeview() { String a=""; final LinearLayout linearLayout=findViewById(R.id.linearlayout); sqL = new SQL(MainActivity.this, "Jizhangben", null, 1); sqliteDatabase = sqL.getReadableDatabase(); Cursor c = sqliteDatabase.rawQuery("select * from jizhangben order by time desc", null); if(c.moveToNext()) { c.moveToFirst(); do { a=c.getString(c.getColumnIndex("driction"))+ ":" + c.getString(c.getColumnIndex("money")) +"元"+","+"备注:" + c.getString(c.getColumnIndex("info"))+ ","+ "时间:" + c.getString(c.getColumnIndex("time")); TextView t = new TextView(this); t.setText(a); t.setTextSize(20); t.setPadding(0, 0, 0, 15); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); t.setLayoutParams(params); linearLayout.addView(t); } while (c.moveToNext()); }else{ final TextView textView = new TextView(this); textView.setText("无记录"); textView.setTextSize(19); textView.setPadding(0, 0, 0, 10); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView.setLayoutParams(params); linearLayout.addView(textView); } } @Override public boolean onCreatePanelMenu(int featureId, Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); MenuItem deleteItem = menu.findItem(R.id.delete); deleteItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { deleteall(); return false; } }); return super.onCreatePanelMenu(featureId, menu); } public void deleteall(){ LinearLayout b=findViewById(R.id.linearlayout); AlertDialog alertDialog=new AlertDialog.Builder(MainActivity.this).create(); alertDialog.setTitle("提示"); alertDialog.setMessage("清空数据?"); alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { sqL.getWritableDatabase().execSQL("delete from jizhangben"); Toast.makeText(MainActivity.this,"清空成功",Toast.LENGTH_SHORT).show(); finish(); Intent intent = new Intent(MainActivity.this, MainActivity.class); startActivity(intent); } }); alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); alertDialog.show(); } }
package com.JIN; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Date; public class add_Activity extends AppCompatActivity { private String driction=""; private SQL sqL; private SQLiteDatabase sqliteDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); TextView textView =findViewById(R.id.commit); RadioGroup radioGroup=findViewById(R.id.driction); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton r=findViewById(checkedId); driction=r.getText().toString(); } }); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String time = new SimpleDateFormat("yyyy/MM/dd,HH:mm:ss").format(new Date()); EditText money=findViewById(R.id.money); EditText info=findViewById(R.id.info); if(money.getText().toString().equals("")||money.getText().toString().equals("")) { Toast.makeText(add_Activity.this,"填写完整即可提交",Toast.LENGTH_SHORT).show(); }else{ sqL=new SQL(add_Activity.this,"Jizhangben",null,1); sqliteDatabase = sqL.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("money", money.getText().toString()); values.put("time", time); values.put("driction", driction); values.put("info", info.getText().toString()); sqliteDatabase.insert("jizhangben", null, values); sqliteDatabase.close(); money.setText(""); info.setText(""); Toast.makeText(add_Activity.this,"添加成功!",Toast.LENGTH_SHORT).show(); } } }); } }
package com.JIN; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.TextView; public class firstActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar actionBar=getSupportActionBar(); actionBar.hide(); setContentView(R.layout.activity_first); TextView t1=findViewById(R.id.add); TextView t2=findViewById(R.id.look); t2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(firstActivity.this, MainActivity.class); startActivity(intent); } }); t1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(firstActivity.this, add_Activity.class); startActivity(intent); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_HOME)) && event.getRepeatCount() == 0) { dialog_Exit(firstActivity.this); } return false; } private void dialog_Exit(Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("确定要退出吗?"); builder.setTitle("提示"); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); System.exit(0); finish(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } }
public void onCreate(SQLiteDatabase db) { db.execSQL(add); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i("记账本","--版本更新"+oldVersion+"-->"+newVersion); String sql = "alter table tally add sex varchar(8)"; db.execSQL(sql); } }