【问题标题】:Uable to setText on EditText in dialogbox无法在对话框的 EditText 中设置文本
【发布时间】: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>
           

【问题讨论】:

    标签: android kotlin settext


    【解决方案1】:

    你不能直接从String转换成Editable,使用

    private fun String.toEditable(): Editable =  
        Editable.Factory.getInstance().newEditable(this)
    

    在您的活动中作为实用函数并应用为

    etDownGame.setText(savedString.toEditable())
    

    【讨论】:

    • 非常感谢@Anshul。我实施了您的建议,但仍未将值(即 saveString“test15”)传递给 editText。它仍然显示提示而不是字符串值“test15)
    • 尝试使用etDownGame.text = savedString.toEditable()
    【解决方案2】:

    再次感谢安舒尔。它在将 etDownGame 移出 showAlertDialog 后工作。虽然我仍然不确定为什么 setText() 在 showAlertDialog 中不起作用。

    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, "v8 savedString = $savedString, length ${savedString!!.length}", Toast.LENGTH_LONG).show()
    
            val boardDownloadView = LayoutInflater.from( this).inflate(R.layout.dialog_download_board, null)
            val etDownGame = boardDownloadView.findViewById<EditText>(R.id.etDownloadGame)
            if (savedString!!.isNotEmpty()) {
                etDownGame.setText(savedString)
            }
    
    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)
        
    
                gameToDownload = etDownGame.text.toString().trim()
    
                    downloadGame(gameToDownload)
            })
        }
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2011-10-01
      • 2013-07-02
      • 1970-01-01
      • 2014-12-15
      • 2015-12-17
      相关资源
      最近更新 更多