AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:configChanges="keyboard|keyboardHidden|orientation|screenSize" android:theme="@style/Theme.MyApplication" tools:targetApi="31"> <!-- <activity--> <!-- android:name=".MainActivity"--> <!-- android:exported="true"--> <!-- android:label="@string/app_name"--> <!-- android:theme="@style/Theme.MyApplication.NoActionBar">--> <!-- <intent-filter>--> <!-- <action android:name="android.intent.action.MAIN" />--> <!-- <category android:name="android.intent.category.LAUNCHER" />--> <!-- </intent-filter>--> <!-- </activity>--> <activity android:name=".AddWordActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.MyApplication.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.MyApplication.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".RenewWordActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.MyApplication.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ShowWordActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.MyApplication.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
AddWordActivity.java
package com.example.myapplication; import android.content.Intent; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import androidx.appcompat.app.AppCompatActivity; public class AddWordActivity extends AppCompatActivity { private EditText et_dc, et_fy, et_zj; private Spinner spCategory; private DatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_word); dbHelper = new DatabaseHelper(this); et_dc = findViewById(R.id.et_dc); et_fy = findViewById(R.id.et_fy); et_zj = findViewById(R.id.et_zj); spCategory = findViewById(R.id.sp_category); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.categories, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spCategory.setAdapter(adapter); Button btnAdd = findViewById(R.id.btn_add); Button btnBack = findViewById(R.id.btn_back); btnAdd.setOnClickListener(v -> { String dc = et_dc.getText().toString(); String fy = et_fy.getText().toString(); String cx = spCategory.getSelectedItem().toString(); String zj = et_zj.getText().toString(); Word word = new Word(-1, dc, fy, cx, zj); dbHelper.insertWord(word); Intent intent = new Intent(AddWordActivity.this, ShowWordActivity.class); startActivity(intent); }); btnBack.setOnClickListener(v -> { Intent intent = new Intent(AddWordActivity.this,MainActivity.class); startActivity(intent); }); } }
DatabaseHelper.java
package com.example.myapplication; import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList; public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "wordstore.db"; private static final int DATABASE_VERSION = 1; public static final String TABLE_WORDS = "words"; public static final String COLUMN_ID = "id"; public static final String COLUMN_DC = "dc"; public static final String COLUMN_FY = "fy"; public static final String COLUMN_CX = "cx"; public static final String COLUMN_ZJ = "zj"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_WORDS + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DC + " TEXT, " + COLUMN_FY + " TEXT, " + COLUMN_CX + " TEXT, " + COLUMN_ZJ + " TEXT "+")"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORDS); onCreate(db); } public void insertWord(Word word) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_DC, word.getDc()); values.put(COLUMN_FY, word.getFy()); values.put(COLUMN_CX, word.getCx()); values.put(COLUMN_ZJ, word.getZj()); db.insert(TABLE_WORDS, null, values); } public void updateWord(Word word){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_DC, word.getDc()); values.put(COLUMN_FY, word.getFy()); values.put(COLUMN_CX, word.getCx()); values.put(COLUMN_ZJ, word.getZj()); String[] whereArgs= { String.valueOf(word.getId()) }; db.update(TABLE_WORDS, values, COLUMN_ID + " = ?", whereArgs); } public void deleteWord(int id){ SQLiteDatabase db = this.getWritableDatabase(); String[] whereArgs= { String.valueOf(id) }; db.delete(TABLE_WORDS,COLUMN_ID + " = ?", whereArgs); } public ArrayList<Word> getAllWords() { ArrayList<Word> words = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_WORDS, null, null, null, null, null, COLUMN_ID + " ASC"); if (cursor.moveToFirst()) { do { @SuppressLint("Range") Word word = new Word( cursor.getInt(cursor.getColumnIndex(COLUMN_ID)), cursor.getString(cursor.getColumnIndex(COLUMN_DC)), cursor.getString(cursor.getColumnIndex(COLUMN_FY)), cursor.getString(cursor.getColumnIndex(COLUMN_CX)), cursor.getString(cursor.getColumnIndex(COLUMN_ZJ))); words.add(word); } while (cursor.moveToNext()); } cursor.close(); return words; } }
MainActivity.java
package com.example.myapplication; import android.content.Intent; import android.os.Bundle; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AppCompatActivity; import android.view.View; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.example.myapplication.databinding.ActivityMainBinding; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_add=findViewById(R.id.btn_add); Button btn_del=findViewById(R.id.btn_del); Button btn_ren=findViewById(R.id.btn_ren); Button btn_que=findViewById(R.id.btn_que); btn_add.setOnClickListener(v -> { Intent intent=new Intent(MainActivity.this,AddWordActivity.class); startActivity(intent); }); btn_del.setOnClickListener(v -> { Intent intent=new Intent(MainActivity.this,ShowWordActivity.class); startActivity(intent); }); btn_ren.setOnClickListener(v -> { Intent intent=new Intent(MainActivity.this,RenewWordActivity.class); startActivity(intent); }); btn_que.setOnClickListener(v -> { Intent intent=new Intent(MainActivity.this,ShowWordActivity.class); startActivity(intent); }); } }
RenewWordActivity.java
package com.example.myapplication; import android.content.Intent; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import androidx.appcompat.app.AppCompatActivity; import java.util.concurrent.locks.ReadWriteLock; public class RenewWordActivity extends AppCompatActivity { private EditText dc2,fy2,id2, zj2; private Spinner spCategory2; private DatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_renew_word); dbHelper = new DatabaseHelper(this); id2=findViewById(R.id.et_id2); dc2 = findViewById(R.id.et_dc2); fy2 = findViewById(R.id.et_fy2); zj2 = findViewById(R.id.et_zj2); spCategory2 = findViewById(R.id.sp_category2); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.categories, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spCategory2.setAdapter(adapter); Button btn_ren = findViewById(R.id.btn_ren); Button btnBack2 = findViewById(R.id.btn_back2); btn_ren.setOnClickListener(v -> { int id= Integer.parseInt(id2.getText().toString()); String dc = dc2.getText().toString(); String fy = fy2.getText().toString(); String category = spCategory2.getSelectedItem().toString(); String zj = zj2.getText().toString(); Word word = new Word(id, dc, fy, category, zj); dbHelper.updateWord(word); Intent intent = new Intent(RenewWordActivity.this, ShowWordActivity.class); startActivity(intent); }); btnBack2.setOnClickListener(v -> { Intent intent = new Intent(RenewWordActivity.this,MainActivity.class); startActivity(intent); }); } } ShowWordActivity.java
package com.example.myapplication; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.util.Log; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class ShowWordActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_word); DatabaseHelper dbHelper = new DatabaseHelper(this); ListView lvWords; lvWords = findViewById(R.id.lv_books); ArrayList<Word> words = dbHelper.getAllWords(); ArrayAdapter<Word> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, words); lvWords.setAdapter(adapter); lvWords.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { AlertDialog.Builder builder = new AlertDialog.Builder(ShowWordActivity.this); builder.setPositiveButton("删除", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Word word = words.get(position); dbHelper.deleteWord(word.getId()); words.remove(position); adapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", null); builder.show(); return true; } }); Button btn_back3=findViewById(R.id.btn_back3); btn_back3.setOnClickListener(view -> { Intent intent =new Intent(ShowWordActivity.this,MainActivity.class); startActivity(intent); }); } }
Word.java
package com.example.myapplication; public class Word { private int id; private String dc; private String fy; private String cx; private String zj; public Word(int id, String dc, String fy, String cx, String zj) { this.id = id; this.dc = dc; this.fy = fy; this.cx = cx; this.zj = zj; } // Getters and Setters // ... public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDc() { return dc; } public void setDc(String dc) { this.dc = dc; } public String getFy() { return fy; } public void setFy(String fy) { this.fy = fy; } public String getCx() { return cx; } public void setCx(String cx) { this.cx = cx; } public String getZj() { return zj; } public void setZj(String zj) { this.zj = zj; } @Override public String toString() { return "序号:"+id+"\n单词:"+dc +"\n翻译:" + fy+"\n词性:"+cx+"\n造句:"+zj; } }
image2.jpg
activity_add_word.xml
<?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" android:padding="16dp" android:background = "@drawable/image2" tools:context=".AddWordActivity"> <EditText android:id="@+id/et_dc" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text|textCapSentences|textAutoCorrect|textMultiLine" android:hint="单词" /> <EditText android:id="@+id/et_fy" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text|textCapSentences|textAutoCorrect|textMultiLine" android:hint="翻译" /> <Spinner android:id="@+id/sp_category" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_zj" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text|textCapSentences|textAutoCorrect|textMultiLine" android:hint="造句" /> <Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="添加" /> <Button android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="返回" /> </LinearLayout>
activity_main.xml
<?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" android:padding="16dp" android:background = "@drawable/image2" tools:context=".MainActivity"> <TextView android:id="@+id/title" android:layout_width="390dp" android:layout_height="201dp" android:text=" 百词斩" android:textSize="60sp" android:visibility="visible" /> <Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="添加" /> <Button android:id="@+id/btn_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="删除" /> <Button android:id="@+id/btn_ren" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="更新" /> <Button android:id="@+id/btn_que" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="查询" /> </LinearLayout>
activity_renew_word.xml
<?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" android:padding="16dp" android:background = "@drawable/image2" tools:context=".RenewWordActivity"> <EditText android:id="@+id/et_id2" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text|textCapSentences|textAutoCorrect|textMultiLine" android:hint="id" /> <EditText android:id="@+id/et_dc2" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text|textCapSentences|textAutoCorrect|textMultiLine" android:hint="单词" /> <EditText android:id="@+id/et_fy2" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text|textCapSentences|textAutoCorrect|textMultiLine" android:hint="翻译" /> <Spinner android:id="@+id/sp_category2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_zj2" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text|textCapSentences|textAutoCorrect|textMultiLine" android:hint="造句" /> <Button android:id="@+id/btn_ren" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="更新" /> <Button android:id="@+id/btn_back2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="返回" /> </LinearLayout>
activity_show_word.xml
<?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" android:background = "@drawable/image2" tools:context=".ShowWordActivity"> <Button android:id="@+id/btn_back3" android:layout_width="wrap_content" android:layout_height="42dp" android:layout_gravity="center_horizontal" android:text="返回" /> <ListView android:id="@+id/lv_books" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="purple_200">#FFBB86FC</color> <color name="purple_500">#FF6200EE</color> <color name="purple_700">#FF3700B3</color> <color name="teal_200">#FF03DAC5</color> <color name="teal_700">#FF018786</color> <color name="black">#FF000000</color> <color name="white">#FFFFFFFF</color> </resources>
strings.xml
<resources> <string name="app_name">My Application</string> <string name="action_settings">Settings</string> <!-- Strings used for fragments for navigation --> <string name="first_fragment_label">First Fragment</string> <string name="second_fragment_label">Second Fragment</string> <string name="next">Next</string> <string name="previous">Previous</string> <string name="hello_first_fragment">Hello first fragment</string> <string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string> <string-array name="categories"> <item>名词</item> <item>形容词</item> <item>动词</item> <item>副词</item> <item>介词</item> </string-array> </resources>
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> <!-- Primary brand color. --> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> <!-- Secondary brand color. --> <item name="colorSecondary">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorOnSecondary">@color/black</item> <!-- Status bar color. --> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> <!-- Customize your theme here. --> </style> <style name="Theme.MyApplication.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> <style name="Theme.MyApplication.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="Theme.MyApplication.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> </resources>