安卓开发记单词app单词添加的代码
时间: 2024-01-19 18:03:23 浏览: 31
以下是一个简单的安卓开发记单词app添加单词的代码示例:
首先,在XML布局文件中添加一个EditText用于输入新单词,还有一个Button用于添加单词:
```xml
<EditText
android:id="@+id/editText_new_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="New Word" />
<Button
android:id="@+id/button_add_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Word" />
```
然后,在Java代码中,获取EditText和Button的引用,并在Button上设置点击事件监听器。当用户点击“Add Word”按钮时,获取EditText中的文本,并将其添加到单词列表中:
```java
public class MainActivity extends AppCompatActivity {
private EditText editTextNewWord;
private Button buttonAddWord;
private List<String> wordList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextNewWord = findViewById(R.id.editText_new_word);
buttonAddWord = findViewById(R.id.button_add_word);
buttonAddWord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String newWord = editTextNewWord.getText().toString();
wordList.add(newWord);
editTextNewWord.setText("");
}
});
}
}
```
这段代码使用了一个List<String>来保存单词列表,并在点击“Add Word”按钮时将新单词添加到列表中。添加完成后,清空EditText中的文本以便用户输入下一个单词。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)