android - 如何使用共享偏好来更新另一个活动的数据?
问题描述
我有两个活动 A 和 B。A 是主页,当单击加号按钮时,它转到 B,那里有两个编辑文本(问题和答案)。当两者都输入并单击检查按钮时,它会返回到 A,其中会出现一个新按钮,其中文本设置为输入的问题。我有一个膨胀的视图,允许用户在单击按钮时编辑问题和答案。问题是当我在活动 B 上使用 finish() 时,它会破坏它。我想在活动 A 上使用共享偏好来保存问题 B 上的输入文本,但我不知道如何。有人可以帮忙吗?
活动一:
package com.example.uh
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
lateinit var preferences: SharedPreferences
private val questionActivityCode = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<FloatingActionButton>(R.id.btn2).setOnClickListener{
startActivityForResult(Intent(this@MainActivity, SecondActivity::class.java), questionActivityCode)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == questionActivityCode && resultCode == Activity.RESULT_OK) {
createNewButtonWithText(data?.getStringExtra("test") ?: "")
}
}
private fun createNewButtonWithText(text: String)
{
val newbutton = Button(this@MainActivity)
val layout = findViewById<LinearLayout>(R.id.mainlayout)
newbutton.text = text
newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newbutton.width=1010
newbutton.height=300
newbutton.gravity = Gravity.CENTER
newbutton.translationX= 65F
newbutton.setTextColor(Color.parseColor("#FFFFFFFF"))
newbutton.setBackgroundColor(Color.parseColor("#250A43"))
layout.addView(newbutton)
val inflator = layoutInflater
val builder = AlertDialog.Builder(this)
val intent = Intent(this@MainActivity, SecondActivity::class.java)
preferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
val question = preferences.getString("text", "")
val answer = preferences.getString("text2", "")
if (question != null && answer != null) {
//?
}
else{
//?
}
newbutton.setOnClickListener{
val dialogLayout = inflator.inflate(R.layout.text, null)
with(builder) {
setTitle(newbutton.text)
setPositiveButton("Edit"){dialog, which ->
startActivity(intent)
}
setNegativeButton("Cancel"){dialog, which ->
Log.d("Main", "Negative button clicked")
}
setView(dialogLayout)
show()
}
}
}}
活动 B:
package com.example.uh
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class SecondActivity : AppCompatActivity() {
lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val question = findViewById<EditText>(R.id.question)
val answer = findViewById<EditText>(R.id.answer)
sharedPreferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener {
val questiontext = question.text.toString()
val answertext = answer.text.toString()
val editor:SharedPreferences.Editor = sharedPreferences.edit()
editor.putString("text", questiontext)
editor.putString("text2", answertext)
editor.apply()
val returnIntent = Intent()
returnIntent.putExtra("test", questiontext)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
}
}
解决方案
在您的sharedPreferences
行之后,您可以使用它来设置两个视图的初始文本,如果尚未设置任何内容(例如第一次打开第二个 Activity),则将其默认为空字符串。
sharedPreferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
question.setText(sharedPreferences.getString("text", ""))
answer.setText(sharedPreferences.getString("text2", ""))
推荐阅读
- android - 无法在 Android webview 中播放 Vimeo 视频
- reactjs - Typescript:在测试文件中找不到 React 组件的类型
- selenium - 无法自动化
- python - 使用 pandas 获取数据透视表的代码使用具有相对较小输入矩阵的整个内存
- sas - 如何打印非数值
- c# - 如何将 SQL 函数部署到目标 SQL Server 2016 数据库
- mongodb - 来自数组的 MongoDB 聚合
- batch-file - 如何更改同一文件夹中多个名称相似的文件?
- python - 在 Python 中导入相关模块
- aframe - 具有“阴影”组件的 A-Frame 实体 - 材料“side:double”显示波纹图案