【发布时间】:2021-08-09 07:59:30
【问题描述】:
我有一个在onOptionsItemSelected 中调用的方法showDownloadDialog(),它显示一个带有EditText 的对话框。我希望从SharePreferences 设置这个值。但是.setText() 不显示来自SharePreferences 的字符串值。
toast 显示正确的字符串值。感谢遇到此问题的任何人都可以告诉我正确的路径。非常感谢
private fun showDownloadDialog() {
val sharePreferences: SharedPreferences = getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
val savedString : String? = sharePreferences.getString("STRING_KEY", null)
var gameToDownload: String
Toast.makeText(this, "Prefer string v3 = $savedString, length ${savedString!!.length}", Toast.LENGTH_LONG).show()
val boardDownloadView = LayoutInflater.from( this).inflate(R.layout.dialog_download_board, null)
showAlertDialog(getString(R.string.fetchMemoryGame), boardDownloadView, View.OnClickListener {
// Grab the text of the game name that the user wants to download
val etDownGame = boardDownloadView.findViewById<EditText>(R.id.etDownloadGame)
etDownGame.setText(savedString)
xml如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditText
android:id="@+id/etDownloadGame"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:digits="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"
android:ems="10"
android:hint="@string/enterGameName"
android:text=""
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:maxLines="1"
android:inputType="text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】: