Android studio关于单词本代码实现
时间: 2023-08-14 14:04:32 浏览: 157
以下是一个简单的 Android Studio 单词本应用程序的代码实现。这个应用程序允许用户输入单词和定义,并将其保存到本地数据库中。
首先,我们需要创建一个数据库类来管理我们的数据:
```java
public class DictionaryDatabase extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "dictionary.db";
private static final int DATABASE_VERSION = 1;
public DictionaryDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE words (_id INTEGER PRIMARY KEY AUTOINCREMENT, word TEXT, definition TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS words");
onCreate(db);
}
}
```
这个类创建了一个名为“words”的表,其中包含单词和定义。我们还需要一个类来表示单词和定义:
```java
public class Word {
private long id;
private String word;
private String definition;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getDefinition() {
return definition;
}
public void setDefinition(String definition) {
this.definition = definition;
}
@Override
public String toString() {
return word + ": " + definition;
}
}
```
接下来,我们需要一个活动来显示单词列表和添加新单词。在这个活动中,我们将使用一个 ListView 来显示单词列表,并使用一个对话框来添加新单词。
```java
public class MainActivity extends AppCompatActivity {
private DictionaryDatabase dictionaryDatabase;
private SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dictionaryDatabase = new DictionaryDatabase(this);
displayListView();
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAddDialog();
}
});
}
private void displayListView() {
Cursor cursor = dictionaryDatabase.getReadableDatabase().rawQuery("SELECT * FROM words ORDER BY word ASC", null);
String[] columns = new String[] { "word", "definition" };
int[] to = new int[] { R.id.word, R.id.definition };
dataAdapter = new SimpleCursorAdapter(this, R.layout.word_row, cursor, columns, to, 0);
ListView listView = findViewById(R.id.word_list);
listView.setAdapter(dataAdapter);
}
private void showAddDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add Word");
View view = LayoutInflater.from(this).inflate(R.layout.add_dialog, null);
builder.setView(view);
final EditText wordEditText = view.findViewById(R.id.word_edit_text);
final EditText definitionEditText = view.findViewById(R.id.definition_edit_text);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String word = wordEditText.getText().toString();
String definition = definitionEditText.getText().toString();
ContentValues values = new ContentValues();
values.put("word", word);
values.put("definition", definition);
dictionaryDatabase.getWritableDatabase().insert("words", null, values);
dataAdapter.changeCursor(dictionaryDatabase.getReadableDatabase().rawQuery("SELECT * FROM words ORDER BY word ASC", null));
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
}
```
在这个活动中,我们首先实例化了数据库类,并在 ListView 中显示了单词列表。我们还在“添加”按钮上设置了一个 onClickListener,当用户点击时会显示一个对话框。
最后,我们需要定义单词行的布局和添加对话框的布局:
word_row.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/definition"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
```
add_dialog.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/word_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Word" />
<EditText
android:id="@+id/definition_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Definition" />
</LinearLayout>
```
这是一个非常基本的单词本应用程序的代码实现。你可以根据自己的需求对其进行修改和扩展。
阅读全文