基于Java实现一个简单的单词本Android App的实践
作者:像向日葵一样~ 时间:2023-04-17 21:27:39
本文基于Java实现了一个简单的单词本安卓app,用的是SQLite数据库,包括布局文件、源码及实现图。
布局设计
单词本主界面
<?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);//实例化,创建数据库
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();
}
}
});
}
//插入数据的方法
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);//执行插入操作
renew();
}
private void deleteData(SQLiteDatabase sqLiteDatabase, String word) {
ContentValues values = new ContentValues();
String[] args = {String.valueOf(word)};
sqLiteDatabase.delete("tb_dict", "word=?", args);//执行删除操作
renew();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbOpenHelper != null) {
dbOpenHelper.close();//关闭
}
}
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, "很遗憾,没有相关记录!", 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数据库,Android自带了一种轻量级数据库,使用非常方便。
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)";//定义创建表的
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);//创建单词的数据表
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("词典", "--版本更新" + oldVersion + "-->" + newVersion);
}
}
效果图
来源:https://blog.csdn.net/zhiwenganyong/article/details/122649922
标签:Java,单词本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c# 调用Win32Api关闭当前应用的方法
2023-09-22 20:51:26
fastjson全局日期序列化设置导致JSONField失效问题解决方案
2021-12-13 10:58:24
Java深入讲解二十三种设计模式之中的策略模式
2022-11-25 13:23:15
![](https://img.aspxhome.com/file/2023/5/99855_0s.png)
Android应用开发中Fragment间通信的实现教程
2023-02-23 18:06:46
C#实现猜数字游戏
2021-11-16 07:29:58
![](https://img.aspxhome.com/file/2023/4/91374_0s.jpg)
基于spring AOP @Around @Before @After的区别说明
2023-12-15 03:08:25
![](https://img.aspxhome.com/file/2023/6/81966_0s.jpg)
Android实现点击图片上传SQLite数据库
2021-06-23 04:35:50
Java开发中为什么要使用单例模式详解
2023-12-24 21:56:40
![](https://img.aspxhome.com/file/2023/7/119187_0s.png)
Android ListView 实现上拉加载的示例代码
2021-09-16 18:04:17
![](https://img.aspxhome.com/file/2023/2/126782_0s.gif)
Spring MVC处理方法返回值过程解析
2022-11-05 23:47:14
Android Activity之间的数据传递方法总结
2023-05-26 03:43:21
![](https://img.aspxhome.com/file/2023/4/139474_0s.jpg)
Android 圆角 ImageView类可设置弧度(代码简单)
2022-09-06 13:13:36
C#根据日期计算星期几的实例代码
2021-07-24 21:43:34
springboot如何重定向外部网页
2022-11-12 05:19:19
seata-1.4.0安装及在springcloud中使用详解
2023-02-21 00:49:21
![](https://img.aspxhome.com/file/2023/5/62835_0s.png)
Java实现读取项目中文件(.json或.properties)的方法详解
2022-08-12 23:59:10
详解JAVA高质量代码之数组与集合
2022-03-31 16:42:07
小程序与后端Java接 口交互实现HelloWorld入门
2023-11-04 18:55:31
![](https://img.aspxhome.com/file/2023/4/58864_0s.jpg)
Spring Boot 中的Servlet简单使用
2023-01-28 03:08:06
![](https://img.aspxhome.com/file/2023/9/67139_0s.png)
Maven打包jar生成javadoc文件和source文件代码实例
2021-08-22 21:56:52