android - Kotlin 如何调用扩展函数

标签 android kotlin

我想从另一个类中的 MainActivity 类调用扩展函数。 我该怎么做?

object MainActivity : AppCompatActivity() {

val StringBuilder.readHistory: StringBuilder
    get() {
        val temp = this@readHistory
        temp.setLength(0)
        try {
            val file = InputStreamReader(MainActivity.openFileInput(MainActivity.getString(R.string.dosyaadı)))
            val br = BufferedReader(file)
            var line = br.readLine()
            while (line != null) {
                temp.append(line + "\n")
                line = br.readLine()
            }
            br.close()
            file.close()
} catch (e: Exception) {
            e.printStackTrace()
        }
        return temp
    }

最佳答案

你不能在类之外调用它,因为它嵌套在这个类中,因此只适用于那个范围。

使扩展属性成为顶级(将其移出类)。

关于android - Kotlin 如何调用扩展函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51125445/

相关文章:

android - 方法始终从 Firestore 数据库查询返回 false

php - 如何从php服务器获取json数据到android mobile

即使进行了所有优化,Android Emulator 也会延迟

java - notifyDataSetChanged 不适用于 arraylist 数据类型

Android - 制作 ImageView "blink"

安卓沙漏

android - Adapter.getView Kotlin android 参数 convertView 上的 IllegalArgumentException

android googleMap-如何获取 myLocation 标记的 clickEvent?

android - MessagingStyle 不显示文本

kotlin - Kotlin 中 Float 和 Double 类型的最小值和最大值