【问题标题】:Retrieve view in dialog在对话框中检索视图
【发布时间】:2018-03-15 19:25:18
【问题描述】:

我正在尝试在对话框中操作视图,但我可以检索视图的唯一方法是使用 Java 老式方式,如下所示:

 class MyDialog: DialogFragment() {
        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            val alert = AlertDialog.Builder(context!!)
            val inflater = LayoutInflater.from(activity)
            val view = inflater?.inflate(R.layout.pg_dialog, null)
            with(alert) {
                setView(view)
                setNeutralButton(R.string.scambio_back, DialogInterface.OnClickListener({ _: DialogInterface, _: Int -> dialog.dismiss() }))
            }
            view?.findViewById<TextView>(R.id.dialog_name)?.text = person.name
            view?.findViewById<TextView>(R.id.dialog_surname)?.text = person.surname

            return alert.create()
        }
    }

你知道有什么方法可以在 Kotlin 中检索内部视图,避免 findViewById 吗?

[编辑] 这是 build.gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.project.persons"
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support:recyclerview-v7:27.0.2'
    implementation 'com.android.support:cardview-v7:27.0.2'
    implementation 'com.android.support:design:27.0.2'
    implementation 'com.android.support:support-v4:27.0.2'

    // Firebase
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    implementation 'com.google.firebase:firebase-database:11.8.0'

    implementation 'com.firebaseui:firebase-ui-auth:3.2.2'

    implementation 'com.facebook.android:facebook-android-sdk:4.30.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • 您是否在其他地方使用 Kotlin Android 扩展进行视图查找?这就是你想要做的吗?
  • 我使用 kotlin 扩展,是的
  • 查看我另一个相关问题的答案:stackoverflow.com/a/52965122/5518220

标签: kotlin findviewbyid


【解决方案1】:

在这种情况下,当您不在 FragmentActivity 中,但有一个 View 引用并希望搜索具有给定 ID 的子项时,您可以使用 Kotlin Android Extensions 与此语法:

view.dialog_name.text = person.name
view.dialog_surname.text = person.surname

与使用扩展程序时一样,请确保您有正确的导入。

【讨论】:

  • 我确实使用了 Extensions,但无法找到 view.dialog_name,尽管导入了 kotlinx.android.synthetic.main.pg_dialog。*
  • 您可以发布您的build.gradle 文件吗?可能配置不正确。
  • 感谢您的回答。只是用一个关于如何获得视图的例子来说明一点:val alertDialog = alertDialogBuilderUserInput.create() alertDialog.show() alertDialog.etName.setText("a")
【解决方案2】:

由于你的问题有点不清楚,我想说你指的是做这样的事情:

在您的build.gradle 中导入apply plugin: 'kotlin-android-extensions

然后你必须在你的Activity中导入这个

import kotlinx.android.synthetic.main.activity_pg_dialog.*

例如,如果您有一个TextView,其 ID 为 test,您可以这样做:

test.setText("test text!!")

参考文献:

Documentation Kotlin Android Extensions

Example Kotlin Android Extendions

【讨论】:

  • 您的解决方案确实可以编译,但 dialog_name 为 null 并引发 IllegalStateException。我导入了 kotlinx.android.synthetic.main.pg_dialog.* 并且抛出异常的行是 dialog_name.text = person.name
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2021-02-11
相关资源
最近更新 更多