欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于Java实现一个简单的单词本Android App的实践

程序员文章站 2022-06-10 11:10:20
目录布局设计代码adddanciactivity.javadbopenhelper.java本文基于java实现了一个简单的单词本安卓app,用的是sqlite数据库,包括布局文件、源码及实现图。布局...

        本文基于java实现了一个简单的单词本安卓app,用的是sqlite数据库,包括布局文件、源码及实现图。

布局设计

单词本主界面

基于Java实现一个简单的单词本Android App的实践

<?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);
    }
}

效果图

基于Java实现一个简单的单词本Android App的实践

 到此这篇关于基于java实现一个简单的单词本android app的实践的文章就介绍到这了,更多相关java 单词本内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: Java 单词本