【问题标题】:How to post text to a text box insted of a toast?如何将文本发布到文本框而不是 toast?
【发布时间】:2021-01-19 02:40:34
【问题描述】:

大家好,我是 Android 和 Kotlin 新手,请您帮我解决这个问题。

recognizer.recognize(ink)
            .addOnSuccessListener { result: RecognitionResult ->
                Toast.makeText(context, "I see ${result.candidates[0].text}", Toast.LENGTH_LONG)
                    .show()
            }

这里将结果发布到吐司上,我希望它出现在文本框中怎么做?

【问题讨论】:

  • 这能回答你的问题吗? Setting text in EditText Kotlin
  • 我应该使用 text_view.setText("${result.candidates[0].text}") 而不是 Toast.makeText()。这里 text_view 是我的文本字段的名称?
  • 是的。文本视图应该在你的 XML 布局中声明。
  • 谢谢 nicolas,我不需要在主要活动中做任何事情

标签: android android-studio kotlin android-toast google-mlkit


【解决方案1】:

您需要在布局文件中定义一个 TextView,然后使用您在 onSuccessListener 中获得的检测结果 setText(...)。

【讨论】:

    【解决方案2】:

    您可以使用 SnackBar。将此添加到您的代码中:

        fun showSnackBar(message: String) {
        val snackBar =
            Snackbar.make(findViewById(android.R.id.content), message, 
        Snackbar.LENGTH_LONG)
        val snackBarView = snackBar.view
        snackBarView.setBackgroundColor(
            ContextCompat.getColor(
                this@BaseActivity,
                R.color.PUTCOLORHERE
            )
        )
        snackBar.show()
    }
    

    你可以像这样替换你的吐司:

    recognizer.recognize(ink)
            .addOnSuccessListener { result: RecognitionResult ->
              showSnackBar("PUT WORDS HERE")
            }
    

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多