網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文基于Java實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的單詞本安卓app,用的是SQLite數(shù)據(jù)庫(kù),包括布局文件、源碼及實(shí)現(xiàn)圖。
布局設(shè)計(jì)
單詞本主界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".AddDanciActivity"> <EditText android:id="@+id/addword_edit" android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="20dp" android:hint="單詞:" android:textColor="@android:color/black" android:textColorHint="#DCDCDC" android:textSize="30dp" /> <EditText android:id="@+id/fanyiword_edit" android:layout_width="match_parent" android:layout_height="60dp" android:hint="解釋:" android:textColor="@android:color/black" android:textColorHint="#DCDCDC" android:textSize="30dp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom = "true" android:layout_margin="5dp"> <ListView android:id="@+id/add_list" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="@android:color/black" android:textColorHint="#DCDCDC" android:textSize="30dp" android:layout_above="@id/lineLayout" /> <LinearLayout android:layout_height="50dp" android:layout_width="match_parent" android:id="@+id/lineLayout" android:layout_alignParentBottom="true" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:layout_width="100dp" android:layout_height="50dp" android:id="@+id/add_btn" android:text="添加" /> <Button android:layout_width="100dp" android:layout_height="50dp" android:layout_centerHorizontal="true" android:id="@+id/shanchu_btn" android:layout_gravity="center_vertical" android:text="刪除" /> <Button android:layout_width="100dp" android:layout_height="50dp" android:id="@+id/quxiao_btn" android:layout_gravity="right" android:text="取消" /> </LinearLayout> </RelativeLayout> </LinearLayout>
代碼
AddDanciActivity.java
單詞本主界面的Activity
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.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class AddDanciActivity extends AppCompatActivity { private EditText wordedit; private EditText yisiedit; private Button add_btn; private Button quxiao_btn; private Button shanchu_btn; private ListView listview; private DBOpenHelper dbOpenHelper;//聲明 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_danci); dbOpenHelper = new DBOpenHelper(AddDanciActivity.this, "db_dict", null, 1);//實(shí)例化,創(chuàng)建數(shù)據(jù)庫(kù) wordedit = (EditText) findViewById(R.id.addword_edit); yisiedit = (EditText) findViewById(R.id.fanyiword_edit); listview = (ListView) findViewById(R.id.add_list); add_btn = (Button) findViewById(R.id.add_btn); quxiao_btn = (Button) findViewById(R.id.quxiao_btn); shanchu_btn = (Button) findViewById(R.id.shanchu_btn); quxiao_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(AddDanciActivity.this, "返回單詞本主界面", Toast.LENGTH_SHORT).show(); finish(); } }); shanchu_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String word = wordedit.getText().toString(); String ys = yisiedit.getText().toString(); if (word.equals("")) { Toast.makeText(AddDanciActivity.this, "填寫的單詞為空", Toast.LENGTH_SHORT).show(); } else { deleteData(dbOpenHelper.getReadableDatabase(), word); Toast.makeText(AddDanciActivity.this, "刪除成功", Toast.LENGTH_SHORT).show(); } } }); add_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String word = wordedit.getText().toString(); String ys = yisiedit.getText().toString(); if (word.equals("") || ys.equals("")) { Toast.makeText(AddDanciActivity.this, "填寫的單詞或解釋為空", Toast.LENGTH_SHORT).show(); } else { insertData(dbOpenHelper.getReadableDatabase(), word, ys);//插入生詞 Toast.makeText(AddDanciActivity.this, "添加生詞成功", Toast.LENGTH_SHORT).show(); renew(); } } }); } //插入數(shù)據(jù)的方法 private void insertData(SQLiteDatabase sqLiteDatabase, String word, String ys) { ContentValues values = new ContentValues(); values.put("word", word);//保存單詞 values.put("detail", ys); sqLiteDatabase.insert("tb_dict", null, values);//執(zhí)行插入操作 renew(); } private void deleteData(SQLiteDatabase sqLiteDatabase, String word) { ContentValues values = new ContentValues(); String[] args = {String.valueOf(word)}; sqLiteDatabase.delete("tb_dict", "word=?", args);//執(zhí)行刪除操作 renew(); } @Override protected void onDestroy() { super.onDestroy(); if (dbOpenHelper != null) { dbOpenHelper.close();//關(guān)閉 } } public void renew() { Cursor cursor = dbOpenHelper.getReadableDatabase().query("tb_dict", null, null, null, null, null, null); ArrayList<Map<String, String>> resultList = new ArrayList<Map<String, String>>(); while (cursor.moveToNext()) { Map<String, String> map = new HashMap<String, String>(); map.put("word", cursor.getString(1)); map.put("interpret", cursor.getString(2)); resultList.add(map); } if (resultList == null || resultList.size() == 0) { Toast.makeText(AddDanciActivity.this, "很遺憾,沒有相關(guān)記錄!", Toast.LENGTH_SHORT).show(); } else { SimpleAdapter simpleAdapter = new SimpleAdapter(AddDanciActivity.this, resultList, R.layout.item, new String[]{"word", "interpret" }, new int[]{R.id.textView, R.id.textView2}); listview.setAdapter(simpleAdapter); } } @Override protected void onStart() { super.onStart(); renew(); } }
DBOpenHelper.java
用到的是SQLite數(shù)據(jù)庫(kù),Android自帶了一種輕量級(jí)數(shù)據(jù)庫(kù),使用非常方便。
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import androidx.annotation.Nullable; public class DBOpenHelper extends SQLiteOpenHelper { final String CREATE_TABLE_SQL = "create table tb_dict (_id integer primary key autoincrement,word,detail)";//定義創(chuàng)建表的 public DBOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_SQL);//創(chuàng)建單詞的數(shù)據(jù)表 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i("詞典", "--版本更新" + oldVersion + "-->" + newVersion); } }
效果圖
總結(jié)
原文鏈接:https://blog.csdn.net/zhiwenganyong/article/details/122649922
相關(guān)推薦
- 2022-04-15 python實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)包簽名_python
- 2022-11-12 Python?sklearn分類決策樹方法詳解_python
- 2022-04-24 C++vector的用法你都知道嘛_C 語(yǔ)言
- 2022-08-16 C#?winform?請(qǐng)求http的實(shí)現(xiàn)(get,post)_C#教程
- 2022-05-04 python中使用多線程改進(jìn)flask案例_python
- 2022-06-21 C語(yǔ)言超詳細(xì)講解文件的操作_C 語(yǔ)言
- 2022-09-10 Python?turtle庫(kù)(繪制螺旋正方形)_python
- 2022-06-27 Python使用re模塊實(shí)現(xiàn)okenizer(表達(dá)式分詞器)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支