基于Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的單詞本Android App的實(shí)踐
本文基于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, "填寫(xiě)的單詞為空", 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, "填寫(xiě)的單詞或解釋為空", 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, "很遺憾,沒(méi)有相關(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);
}
}效果圖

到此這篇關(guān)于基于Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的單詞本Android App的實(shí)踐的文章就介紹到這了,更多相關(guān)Java 單詞本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?自定義注解實(shí)現(xiàn)涉密字段脫敏
關(guān)于數(shù)據(jù)脫敏,網(wǎng)上的文章都是硬編碼規(guī)則,比如對(duì)身份證,手機(jī)號(hào),郵件地址等固定寫(xiě)法脫敏。本文在此基礎(chǔ)上,拓展動(dòng)態(tài)從數(shù)據(jù)庫(kù)查出涉密關(guān)鍵字執(zhí)行脫敏操作。感興趣的同學(xué)可以參考閱讀2023-03-03
Java經(jīng)典設(shè)計(jì)模式之策略模式原理與用法詳解
這篇文章主要介紹了Java經(jīng)典設(shè)計(jì)模式之策略模式,簡(jiǎn)單說(shuō)明了策略模式的概念、原理并結(jié)合實(shí)例形式分析了java策略模式的具有用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
java代理 jdk動(dòng)態(tài)代理應(yīng)用案列
java代理有jdk動(dòng)態(tài)代理、cglib代理,這里只說(shuō)下jdk動(dòng)態(tài)代理,jdk動(dòng)態(tài)代理主要使用的是java反射機(jī)制,需要了解的朋友可以參考下2012-11-11
Java中十六進(jìn)制和十進(jìn)制之間互相轉(zhuǎn)換代碼示例
這篇文章主要給大家介紹了關(guān)于Java中十六進(jìn)制和十進(jìn)制之間互相轉(zhuǎn)換的相關(guān)資料,我們項(xiàng)目過(guò)程中總是要用到十進(jìn)制與十六進(jìn)制相互轉(zhuǎn)換的方法,需要的朋友可以參考下2023-07-07
用html css javascript打造自己的RIA圖文教程
用html&css&javascript打造自己的RIA之一,包括了配置等2009-07-07
詳解Spring AOP 實(shí)現(xiàn)主從讀寫(xiě)分離
本篇文章主要介紹了Spring AOP 實(shí)現(xiàn)主從讀寫(xiě)分離,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Spring?AOP手寫(xiě)動(dòng)態(tài)代理代碼實(shí)例
這篇文章主要介紹了Spring?AOP手寫(xiě)動(dòng)態(tài)代理代碼實(shí)例,AOP我們知道,是在不修改源代碼的情況下,為代碼添加一些新功能的技術(shù),通過(guò)動(dòng)態(tài)代理,可以在不修改原始類(lèi)代碼的前提下,對(duì)方法進(jìn)行攔截和增強(qiáng),需要的朋友可以參考下2024-01-01

