如何在android中获取ObservableField的值

时间:2017-10-22 08:14:03

标签: android kotlin rx-java rx-android android-databinding

您好我的java代码中有ObservableField。我希望得到它的价值,可以通过调用get方法来实现。

val email = ObservableField<String>()

这可以使用以下方法完成。我很困惑,不知道我应该在这里取一个吸气剂来获得它的价值吗?或者有不同的标准方法来获取ObservableField我在我的应用程序中使用RxJava的值。

fun login(view: View) {
    val emailVal = email.get()
}

2 个答案:

答案 0 :(得分:4)

这正是代表团的意义所在。 Kotlin中的属性委派意味着有一个实现操作符函数getValue和可选setValue的类,在访问或更新属性时将调用它。

您的代表可能如下所示:

class <T> ObservableDelegate
{
    val field = ObservableField<T>()
    operator fun getValue(self: Any?, prop: KProperty<*>) : T
        = field.get()
    operator fun setValue(self: Any?, prop: KProperty<*>, value: T)
        = field.set(value)
}

然后您可以像这样使用委托:

val email : String by ObservableDelegate()

fun login(view: View) {
    val emailVal = email
}

在此处阅读有关属性委派的更多信息:https://kotlinlang.org/docs/reference/delegated-properties.html

答案 1 :(得分:1)

我认为使用Xcode build... 2017-10-22 14:14:29.951 xcodebuild[69327:634958] [MT] IDEDistribution: -[IDEDistributionLogging _createLoggingBundleAtPath:]: Created bundle at path '/var/folders/73/29kcfd816cd6jbsm48mqkp2r0000gp/T/questNewS_2017-10-22_14-14-29.950.xcdistributionlogs'. 2017-10-22 14:14:34.389 xcodebuild[69327:634958] [MT] DVTAssertions: Warning in /Library/Caches/com.apple.xbs/Sources/DVTFrameworks/DVTFrameworks-13233/DVTFoundation/DeveloperStructure/DVTPlatformFamily.m:192 Details: Error Domain=DVTMachOErrorDomain Code=1 "Reached end of file while looking for: Mach-O slice." UserInfo={NSLocalizedDescription=Reached end of file while looking for: Mach-O slice., NSLocalizedRecoverySuggestion=} Object: <NSBundle> Method: +dvt_platformFamilyForBundleAtPath:error: Thread: <NSThread: 0x7ff668d15a50>{number = 1, name = main} Please file a bug at http://bugreport.apple.com with this warning message and any useful information you can provide. 2017-10-22 14:14:34.510 xcodebuild[69327:634958] [MT] DVTAssertions: Warning in /Library/Caches/com.apple.xbs/Sources/DVTFrameworks/DVTFrameworks-13233/DVTFoundation/DeveloperStructure/DVTPlatformFamily.m:192 Details: Error Domain=DVTMachOErrorDomain Code=1 "Reached end of file while looking for: Mach-O slice." UserInfo={NSLocalizedDescription=Reached end of file while looking for: Mach-O slice., NSLocalizedRecoverySuggestion=} Object: <NSBundle> Method: +dvt_platformFamilyForBundleAtPath:error: Thread: <NSThread: 0x7ff668d15a50>{number = 1, name = main} Please file a bug at http://bugreport.apple.com with this warning message and any useful information you can provide. 2017-10-22 14:14:34.512 xcodebuild[69327:634958] [MT] DVTAssertions: Warning in /Library/Caches/com.apple.xbs/Sources/DVTFrameworks/DVTFrameworks-13233/DVTFoundation/DeveloperStructure/DVTPlatformFamily.m:192 Details: Error Domain=DVTMachOErrorDomain Code=1 "Reached end of file while looking for: Mach-O slice." UserInfo={NSLocalizedDescription=Reached end of file while looking for: Mach-O slice., NSLocalizedRecoverySuggestion=} Object: <NSBundle> Method: +dvt_platformFamilyForBundleAtPath:error: Thread: <NSThread: 0x7ff668d15a50>{number = 1, name = main} Please file a bug at http://bugreport.apple.com with this warning message and any useful information you can provide. 2017-10-22 14:14:34.513 xcodebuild[69327:634958] [MT] DVTAssertions: Warning in /Library/Caches/com.apple.xbs/Sources/DVTFrameworks/DVTFrameworks-13233/DVTFoundation/DeveloperStructure/DVTPlatformFamily.m:192 Details: Error Domain=DVTMachOErrorDomain Code=1 "Reached end of file while looking for: Mach-O slice." UserInfo={NSLocalizedDescription=Reached end of file while looking for: Mach-O slice., NSLocalizedRecoverySuggestion=} Object: <NSBundle> Method: +dvt_platformFamilyForBundleAtPath:error: Thread: <NSThread: 0x7ff668d15a50>{number = 1, name = main} Please file a bug at http://bugreport.apple.com with this warning message and any useful information you can provide. 2017-10-22 14:14:34.863 xcodebuild[69327:634958] [MT] IDEDistribution: Step failed: <IDEDistributionSigningAssetsStep: 0x7ff66e134cc0>: Error Domain=IDEDistributionSigningAssetStepErrorDomain Code=0 "Locating signing assets failed." UserInfo={NSLocalizedDescription=Locating signing assets failed., IDEDistributionSigningAssetStepUnderlyingErrors=( "Error Domain=IDEProvisioningErrorDomain Code=8 \"adb has conflicting provisioning settings.\" UserInfo={NSLocalizedDescription=adb has conflicting provisioning settings., NSLocalizedRecoverySuggestion=adb is automatically signed for distribution, but a conflicting code signing identity iPhone Developer has been manually specified. Remove the \"signingCertificate\" entry from your Export Options property list, or switch to manual signing by setting \"signingStyle\" to \"Manual.}", "Error Domain=IDEProvisioningErrorDomain Code=8 \"ios-device-lib has conflicting provisioning settings.\" UserInfo={NSLocalizedDescription=ios-device-lib has conflicting provisioning settings., NSLocalizedRecoverySuggestion=ios-device-lib is automatically signed for distribution, but a conflicting code signing identity iPhone Developer has been manually specified. Remove the \"signingCertificate\" entry from your Export Options property list, or switch to manual signing by setting \"signingStyle\" to \"Manual.}" )} error: exportArchive: adb has conflicting provisioning settings. Error Domain=IDEProvisioningErrorDomain Code=8 "adb has conflicting provisioning settings." UserInfo={NSLocalizedDescription=adb has conflicting provisioning settings., NSLocalizedRecoverySuggestion=adb is automatically signed for distribution, but a conflicting code signing identity iPhone Developer has been manually specified. Remove the "signingCertificate" entry from your Export Options property list, or switch to manual signing by setting "signingStyle" to "Manual.} error: exportArchive: ios-device-lib has conflicting provisioning settings. Error Domain=IDEProvisioningErrorDomain Code=8 "ios-device-lib has conflicting provisioning settings." UserInfo={NSLocalizedDescription=ios-device-lib has conflicting provisioning settings., NSLocalizedRecoverySuggestion=ios-device-lib is automatically signed for distribution, but a conflicting code signing identity iPhone Developer has been manually specified. Remove the "signingCertificate" entry from your Export Options property list, or switch to manual signing by setting "signingStyle" to "Manual.} ** EXPORT FAILED ** Unable to apply changes on device: 2f5eb251a28d7102c8c421f1f8477b21f67a1470. Error is: Command xcodebuild failed with exit code 70 已经足够了。如果您真的想在代码中消除email.get()的使用,可以使用支持字段:

.get()