android - 如何在 kotlin 中将 View 对象转换为我的布局

标签 android kotlin

如何在setAnswer函数中访问page.questionTextView

//Create the given page (indicated by position)
override fun instantiateItem(container: ViewGroup, position: Int): Any {
    val page = inflater.inflate(R.layout.layout_question, null)
    page.questionTextView.text = "hi"

    //Add the page to the front of the queue
    (container as ViewPager).addView(page, 0)
    return page
}

private fun setAnswer(page: View){
    //how to access page.questionTextView
}

最佳答案

我找到了解决方案:我使用“with”方法访问 page.questionTextView

private fun setAnswer(page: View){
    with(page){
        questionTextView.text = "hi"
    }
}

关于android - 如何在 kotlin 中将 View 对象转换为我的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55163942/

相关文章:

android - 如何按 buildType AND flavor 拆分源代码

kotlin - 卡夫卡流: groupByKey and reduce not triggering action exactly once when error occurs in stream

android - Kotlin:元素未附加在列表中

android - 当隐藏状态栏时,有没有办法阻止 "jumping"的布局?

java - 如果我在 kotlin 中实现如下网络检查,会出现什么情况?

android - 如何限制 DatePicker 上的用户只能选择 today_date 之前的日期 - 18 年

android - 使用新的默认项目选项获得完整的 javadoc 支持的最少步骤数是多少?

android - Room SQLite 数据库 - 删除所有 LIKE % :param%

android - (Android) D/OkHttp : 400 Bad Request

android - 断开 USB 后,ADB over WiFi 连接丢失