gpt4 book ai didi

kotlin - 在对话框中检索 View

转载 作者:行者123 更新时间:2023-12-02 13:38:42 25 4
gpt4 key购买 nike

我正在尝试在对话框中操作 View ,但我可以检索 View 的唯一方法是使用 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 中检索内部 View ,避免 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'

最佳答案

在这种情况下,当您不在 Fragment 中时或 Activity ,但有 View如果您想搜索具有给定 ID 的子项,则可以使用 Kotlin Android Extensions 和以下语法:

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

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

关于kotlin - 在对话框中检索 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com