使用Activity、Intent、事件处理等开发记单词app的代码
时间: 2023-12-10 16:05:34 浏览: 100
安卓记单词app项目设计
以下是一个使用Activity、Intent、事件处理等开发记单词app的基本代码示例:
MainActivity.java:
```
public class MainActivity extends AppCompatActivity {
private EditText wordInput;
private Button addWordButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wordInput = findViewById(R.id.word_input);
addWordButton = findViewById(R.id.add_word_button);
addWordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String word = wordInput.getText().toString();
if (!TextUtils.isEmpty(word)) {
addWordToList(word);
wordInput.setText("");
}
}
});
}
private void addWordToList(String word) {
WordDatabaseHelper dbHelper = new WordDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(WordDatabaseHelper.COLUMN_NAME_WORD, word);
db.insert(WordDatabaseHelper.TABLE_NAME, null, values);
db.close();
Toast.makeText(this, "Word added to list.", Toast.LENGTH_SHORT).show();
}
public void openWordList(View view) {
Intent intent = new Intent(this, WordListActivity.class);
startActivity(intent);
}
}
```
WordListActivity.java:
```
public class WordListActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private WordListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_list);
recyclerView = findViewById(R.id.word_list_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new WordListAdapter(this);
recyclerView.setAdapter(adapter);
}
}
```
WordListAdapter.java:
```
public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.ViewHolder> {
private List<String> wordList;
private Context context;
public WordListAdapter(Context context) {
this.context = context;
wordList = getWordList();
}
private List<String> getWordList() {
List<String> list = new ArrayList<>();
WordDatabaseHelper dbHelper = new WordDatabaseHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] projection = {WordDatabaseHelper.COLUMN_NAME_WORD};
Cursor cursor = db.query(WordDatabaseHelper.TABLE_NAME, projection, null, null, null, null, null);
while (cursor.moveToNext()) {
String word = cursor.getString(cursor.getColumnIndexOrThrow(WordDatabaseHelper.COLUMN_NAME_WORD));
list.add(word);
}
cursor.close();
db.close();
return list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.word_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final String word = wordList.get(position);
holder.wordTextView.setText(word);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, WordDetailActivity.class);
intent.putExtra(WordDetailActivity.EXTRA_WORD, word);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return wordList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView wordTextView;
public ViewHolder(View itemView) {
super(itemView);
wordTextView = itemView.findViewById(R.id.word_text_view);
}
}
}
```
WordDetailActivity.java:
```
public class WordDetailActivity extends AppCompatActivity {
public static final String EXTRA_WORD = "com.example.myapp.WORD";
private TextView wordTextView;
private EditText definitionEditText;
private Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_detail);
wordTextView = findViewById(R.id.word_text_view);
definitionEditText = findViewById(R.id.definition_edit_text);
saveButton = findViewById(R.id.save_button);
String word = getIntent().getStringExtra(EXTRA_WORD);
wordTextView.setText(word);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String definition = definitionEditText.getText().toString();
if (!TextUtils.isEmpty(definition)) {
saveDefinition(wordTextView.getText().toString(), definition);
Toast.makeText(WordDetailActivity.this, "Definition saved.", Toast.LENGTH_SHORT).show();
}
}
});
}
private void saveDefinition(String word, String definition) {
WordDatabaseHelper dbHelper = new WordDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(WordDatabaseHelper.COLUMN_NAME_DEFINITION, definition);
String selection = WordDatabaseHelper.COLUMN_NAME_WORD + " LIKE ?";
String[] selectionArgs = { word };
db.update(WordDatabaseHelper.TABLE_NAME, values, selection, selectionArgs);
db.close();
}
}
```
WordDatabaseHelper.java:
```
public class WordDatabaseHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "WordList.db";
public static final String TABLE_NAME = "word";
public static final String COLUMN_NAME_WORD = "word";
public static final String COLUMN_NAME_DEFINITION = "definition";
public static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_NAME_WORD + " TEXT PRIMARY KEY," +
COLUMN_NAME_DEFINITION + " TEXT)";
public static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + TABLE_NAME;
public WordDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
```
注意:这只是一个简单的代码示例,你可以根据你的实际需求和技术水平进行修改和优化。
阅读全文