0

如何在 kotlin 中更新 Textview 的文本。Set Text on-create 功能有效,但是当我在 main fun 之外尝试时,它显示未解析的 ref。

如何声明小部件以重用 TextView 以更新文本值?我没有 kotlin 的经验。有人可以帮助我吗?

class MediaPickedActivity : AppCompatActivity() {

    val fullName = "Test User"
    var score = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_media_picked)

        val Tv_test = findViewById(R.id.tv_text) as TextView

        Tv_test.setText("$fullName :: $score ")

        if (score in 0..300) {
            score = 5
            setText()
        }

    }

    private fun setText() {
        // Error is here. I can't set text.
        Tv_test.setText("$fullName :: $score ")
    }

}

在此处输入图像描述

4

3 回答 3

0

您应该将您View的 s 声明为类级别的属性,然后您可以从类中的任何位置访问它们。在函数内声明的变量只能在该函数中访问。

class MediaPickedActivity : AppCompatActivity() {
    lateinit var Tv_test: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_media_picked)

        Tv_test = findViewById(R.id.tv_text) as TextView
    }

    private fun setText() {
        Tv_test.setText("$fullName :: $score ")
    }
}

我在此示例中使用过,但请参阅此处lateinit有关声明属性的不同方法的详细讨论。

于 2017-07-04T12:33:22.133 回答
0

除了@zsmb13 所说的之外,您还可以使用Kotlin Android Extensions插件(因为您将其作为主题之一包含在内),这对于最大限度地减少潜在findViewById()错误非常方便,不包括使用视图字段/成员变量等。如下:

首先在本地 build.gradle 文件中应用插件:

apply plugin: 'kotlin-android-extensions'

接下来,在 Activity 类中导入特定布局的小部件属性:

import kotlinx.android.synthetic.main.<layout>.*

...然后您的活动将如下所示:

import kotlinx.android.synthetic.main.activity_media_picked.*

class MediaPickedActivity : AppCompatActivity() {

    val fullName = "Test User"
    var score = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_media_picked)

        // Simply references via the TextView's ID, and sets its text.
        tv_text.setText("$fullName :: $score ")

        if (score in 0..300) {
            score = 5
            setText()
        }

    }

    private fun setText() {
        tv_text.setText("$fullName :: $score ")
    }

}
于 2017-07-04T16:48:20.800 回答
-1

您还可以使用惰性属性,该属性将需要时初始化该属性,并将初始化与您的逻辑分开,例如:

class MediaPickedActivity : AppCompatActivity() {
    val Tv_test by lazy { findViewById(R.id.tv_text) as TextView }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_media_picked)

        Tv_test.setText("$fullName :: $score ")
        // ...
    }

    private fun setText() {
        Tv_test.setText("$fullName :: $score ")
    }

}

或者编写一个带有具体类型参数 T的内联函数,以使代码更具可读性。

class MediaPickedActivity : AppCompatActivity() {
    val nonNull by find<TextView>(R.id.tv_text)
    val nullable by find<TextView?>(R.id.tv_text)
    // ...
}

inline fun <reified T : View?> Activity.find(id: Int): Lazy<T> {
   return lazy { findViewById(id) as T }
}
于 2017-07-04T12:48:25.053 回答