创建一个背单词页面,包括一个单词和它的定义的显示框,一个“不认识”按钮和一个“认识”按钮。在该页面上使用API获取例句,并显示在页面上。 当用户点击“不认识”按钮时,切换到下一个单词。当用户点击“认识”按钮时,将该单词添加到已背单词列表中,并自动切换到下一个单词。 添加一个“加入生词本”按钮,将当前单词添加到生词本列表中。生词本可以保存在SQLite中。 创建一个“查看生词列表”页面,显示所有生词。在该页面上,可以删除特定的生词,也可以清空整个生词本列表。 添加一个“查看已背单词”页面,显示所有已经背过的单词。在该页面上,显示每个单词的单词和定义,并可以添加删除按钮,让用户可以从已背单词列表中删除单词。 添加一个联网搜索单词功能,使用一个在线字典API,例如Merriam-Webster或Oxford Dictionary API。当用户输入一个单词时,使用API来搜索该单词的定义,使用Retrofit和Gson库来处理API请求和响应。 使用Kotlin编写代码,使用MVVM架构模式将数据和UI分离,使其更易于维护和扩展。
时间: 2024-02-12 21:06:17 浏览: 123
好的,下面是一个Kotlin代码的示例实现:
1. 创建一个名为WordActivity的活动,用于显示单词和定义:
```kotlin
class WordActivity : AppCompatActivity() {
private var wordList = mutableListOf<Word>()
private var currentIndex = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_word)
getWords()
}
private fun getWords() {
// 使用API获取单词列表
val apiService = ApiClient.create()
apiService.getWords().enqueue(object : Callback<List<Word>> {
override fun onResponse(call: Call<List<Word>>, response: Response<List<Word>>) {
if (response.isSuccessful) {
wordList = response.body()!!.toMutableList()
showWord(wordList[currentIndex])
}
}
override fun onFailure(call: Call<List<Word>>, t: Throwable) {
Toast.makeText(this@WordActivity, "获取单词失败", Toast.LENGTH_SHORT).show()
}
})
}
private fun showWord(word: Word) {
tvWord.text = word.word
tvDefinition.text = word.definition
// 使用API获取例句
val apiService = ApiClient.create()
apiService.getExample(word.word).enqueue(object : Callback<Example> {
override fun onResponse(call: Call<Example>, response: Response<Example>) {
if (response.isSuccessful) {
tvExample.text = response.body()!!.text
}
}
override fun onFailure(call: Call<Example>, t: Throwable) {
tvExample.text = "例句获取失败"
}
})
}
fun onKnow(view: View) {
// 将当前单词添加到已背单词列表中
addKnownWord(wordList[currentIndex])
// 显示下一个单词
currentIndex++
if (currentIndex < wordList.size) {
showWord(wordList[currentIndex])
} else {
Toast.makeText(this, "已背完所有单词", Toast.LENGTH_SHORT).show()
finish()
}
}
fun onUnknow(view: View) {
// 显示下一个单词
currentIndex++
if (currentIndex < wordList.size) {
showWord(wordList[currentIndex])
} else {
Toast.makeText(this, "已背完所有单词", Toast.LENGTH_SHORT).show()
finish()
}
}
private fun addKnownWord(word: Word) {
// 将单词添加到已背单词列表中
val db = AppDatabase.getInstance(this)
db.wordDao().insertWord(word)
}
fun onAddToVocabulary(view: View) {
// 将当前单词添加到生词本列表中
val db = AppDatabase.getInstance(this)
db.wordDao().insertVocabularyWord(wordList[currentIndex])
Toast.makeText(this, "生词已添加", Toast.LENGTH_SHORT).show()
}
fun onViewVocabulary(view: View) {
// 跳转到查看生词本列表页面
val intent = Intent(this, VocabularyActivity::class.java)
startActivity(intent)
}
fun onViewKnown(view: View) {
// 跳转到查看已背单词列表页面
val intent = Intent(this, KnownActivity::class.java)
startActivity(intent)
}
}
```
2. 创建一个名为Word的数据类,用于保存单词和定义:
```kotlin
data class Word(
val word: String,
val definition: String
)
```
3. 创建一个名为Example的数据类,用于保存例句:
```kotlin
data class Example(
val text: String
)
```
4. 创建一个ApiClient类,用于创建Retrofit实例并定义API接口:
```kotlin
object ApiClient {
private const val BASE_URL = "https://api.dictionary.com/"
private val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
fun create(): ApiService {
return retrofit.create(ApiService::class.java)
}
}
interface ApiService {
@GET("words")
fun getWords(): Call<List<Word>>
@GET("example/{word}")
fun getExample(@Path("word") word: String): Call<Example>
}
```
5. 创建一个名为AppDatabase的类,用于创建SQLite数据库:
```kotlin
@Database(entities = [Word::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun wordDao(): WordDao
companion object {
private var instance: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"word_database"
).build()
}
return instance!!
}
}
}
@Entity(tableName = "known_words")
data class KnownWord(
@PrimaryKey val word: String,
val definition: String
)
@Entity(tableName = "vocabulary_words")
data class VocabularyWord(
@PrimaryKey val word: String,
val definition: String
)
@Dao
interface WordDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertWord(word: Word)
@Query("SELECT * FROM known_words")
fun getKnownWords(): List<KnownWord>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertVocabularyWord(word: Word)
@Query("SELECT * FROM vocabulary_words")
fun getVocabularyWords(): List<VocabularyWord>
@Query("DELETE FROM vocabulary_words")
fun deleteVocabularyWords()
@Query("DELETE FROM known_words WHERE word = :word")
fun deleteKnownWord(word: String)
}
```
6. 创建一个名为VocabularyActivity的活动,用于显示生词本列表:
```kotlin
class VocabularyActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: WordAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_vocabulary)
recyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
adapter = WordAdapter()
recyclerView.adapter = adapter
getWords()
}
private fun getWords() {
// 从SQLite中获取生词本列表
val db = AppDatabase.getInstance(this)
val words = db.wordDao().getVocabularyWords()
adapter.setData(words)
}
fun onDeleteAll(view: View) {
// 清空生词本列表
val db = AppDatabase.getInstance(this)
db.wordDao().deleteVocabularyWords()
adapter.setData(emptyList())
}
inner class WordAdapter : RecyclerView.Adapter<WordAdapter.ViewHolder>() {
private var wordList = listOf<VocabularyWord>()
fun setData(words: List<VocabularyWord>) {
wordList = words
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_word, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val word = wordList[position]
holder.tvWord.text = word.word
holder.tvDefinition.text = word.definition
holder.btnDelete.setOnClickListener {
// 从生词本列表中删除该单词
val db = AppDatabase.getInstance(this@VocabularyActivity)
db.wordDao().deleteKnownWord(word.word)
wordList = wordList.filter { it.word != word.word }
notifyDataSetChanged()
}
}
override fun getItemCount(): Int {
return wordList.size
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tvWord: TextView = view.findViewById(R.id.tv_word)
val tvDefinition: TextView = view.findViewById(R.id.tv_definition)
val btnDelete: Button = view.findViewById(R.id.btn_delete)
}
}
}
```
7. 创建一个名为KnownActivity的活动,用于显示已背单词列表:
```kotlin
class KnownActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: WordAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_known)
recyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
adapter = WordAdapter()
recyclerView.adapter = adapter
getWords()
}
private fun getWords() {
// 从SQLite中获取已背单词列表
val db = AppDatabase.getInstance(this)
val words = db.wordDao().getKnownWords().map { VocabularyWord(it.word, it.definition) }
adapter.setData(words)
}
inner class WordAdapter : RecyclerView.Adapter<WordAdapter.ViewHolder>() {
private var wordList = listOf<VocabularyWord>()
fun setData(words: List<VocabularyWord>) {
wordList = words
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_word, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val word = wordList[position]
holder.tvWord.text = word.word
holder.tvDefinition.text = word.definition
holder.btnDelete.setOnClickListener {
// 从已背单词列表中删除该单词
val db = AppDatabase.getInstance(this@KnownActivity)
db.wordDao().deleteKnownWord(word.word)
wordList = wordList.filter { it.word != word.word }
notifyDataSetChanged()
}
}
override fun getItemCount(): Int {
return wordList.size
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tvWord: TextView = view.findViewById(R.id.tv_word)
val tvDefinition: TextView = view.findViewById(R.id.tv_definition)
val btnDelete: Button = view.findViewById(R.id.btn_delete)
}
}
}
```
以上就是一个简单的背单词app的Kotlin实现。注意,这只是一个示例,实际开发中还需要考虑很多其他的细节和问题。
阅读全文