editText 获取文本 kotlin [英] editText get text kotlin

查看:98
本文介绍了editText 获取文本 kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 kotlin 中获取 editText 并与 toast 一起显示.

How to get editText in kotlin and display with toast.

var editTextHello = findViewById(R.id.editTextHello)

我试过了,但显示对象

Toast.makeText(this,editTextHello.toString(),Toast.LENGTH_SHORT).show()

推荐答案

您缺少从 findViewByIdEditText<获得的 View 的转换/代码>:

You're missing a cast of the View you get from findViewById to EditText:

var editTextHello = findViewById(R.id.editTextHello) as EditText

然后,您希望在 toast 中显示 EditTexttext 属性:

Then, you want to display the text property of the EditText in your toast:

Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()

<小时>

为了记录,这只是更惯用的 Kotlin 等价于在您的 EditText 上调用 getText(),就像您在 Java 中所做的那样:


For the record, this is just the more idiomatic Kotlin equivalent to calling getText() on your EditText, like you'd do it in Java:

Toast.makeText(this, editTextHello.getText(), Toast.LENGTH_SHORT).show()

这篇关于editText 获取文本 kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆