【问题标题】:How do I access a view from another XML File in an activity如何从活动中的另一个 XML 文件访问视图
【发布时间】:2022-12-13 14:40:11
【问题描述】:

我有一个名为 BaseActivity 的活动,我想从另一个名为 BaseActivity 中的 dialog_progress 的 xml 访问文本视图。我该怎么做

写了下面的代码


private var tvProgressText : Textview? = null

fun showProcessDialog(text: String){
    mProgressDialog = Dialog(this)
    mProgressDialog.setContentView(R.layout.dialog_progress)
    tvProgressText = findViewById(R.id.tv_progress_bar)
    mProgressDialog.tvProgressText = text
    mProgressDialog.show()
}

它给我一个错误,说 tvTextView 未解析的引用

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    progressDialog引用findViewById试试下面的代码

     private var tvProgressText : Textview? = null
    
    fun showProcessDialog(text: String){
        mProgressDialog = Dialog(this)
        mProgressDialog.setContentView(R.layout.dialog_progress)
        tvProgressText = mProgressDialog.findViewById<TextView>(R.id.tv_progress_bar)
        mProgressDialog.tvProgressText = text
        mProgressDialog.show()
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-30
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多