Kotlin:如何使用 Kotlin 在 Android 中获取和设置文本到 TextView? [英] Kotlin: How to get and set a text to TextView in Android using Kotlin?

查看:73
本文介绍了Kotlin:如何使用 Kotlin 在 Android 中获取和设置文本到 TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手.

我想更改TextView的文本,同时点击它.

I want to change text of TextView While click on it.

我的代码:

val text: TextView = findViewById(R.id.android_text) as TextView
    text.setOnClickListener {
        text.setText(getString(R.string.name))
    }

输出:我得到了输出,但显示了使用属性访问语法.

Output: I got the output but showing use property access syntax.

谁能告诉我怎么做?

提前致谢.

推荐答案

在 kotlin 中不要像在 java 中那样使用 getter 和 setter.下面给出了 kotlin 的正确格式.

In kotlin don't use getters and setters as like in java.The correct format of the kotlin is given below.

val textView: TextView = findViewById(R.id.android_text) as TextView
textView.setOnClickListener {
    textView.text = getString(R.string.name)
}

要从 Textview 获取值,我们必须使用此方法

To get the values from the Textview we have to use this method

 val str: String = textView.text.toString()

 println("the value is $str")

这篇关于Kotlin:如何使用 Kotlin 在 Android 中获取和设置文本到 TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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