如何将数据绑定与微调器一起使用并获取当前值?

How to use dataBinding with spinner and get the current value?

我正在使用 MVVM 和数据绑定 (Kotlin) 开发一个 android 应用程序,我想知道如何在我的 ViewModel 中轻松获取微调器的当前值并轻松处理它。 我知道这个问题被问了很多次,但实际上我没有得到有用的答案,所以你能帮助大家吗? 感谢你的帮助。 谢谢。


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="registerviewmodel"
            type="com.AAA.BBB.SignUpAuthViewModel" />
    </data>
<Spinner
                android:id="@+id/citySpinnerSignUpLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:entries="@array/cities"
                />

    </ScrollView>
</layout>
class SignUpAuthViewModel : ViewModel() {

    var username: String? = null
    var password: String? = null
    var town: String? = null
    var age: String? = null
    var phone: String? = null
    var passwordConfirmation: String? = null



//this variable should hold the city of the user
// from spinner of cities
var cityOfUser: String? = null



    fun OnRegisterButtonClicked(view: View) {
    }

    fun onRegisterTextViewClicked(view: View) {
    }
}

您可以使用多个选项。我只会描述两个:

第一个是创建如下所示的 @BindingAdapter

@BindingAdapter("clicks")
fun listenClicks(spinner: AppCompatSpinner, result: ObservableField<String>) {
    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            result.set(parent.getItemAtPosition(position) as String)
        }
    }
}

然后在xml中你将像这样绑定它

<Spinner
                android:id="@+id/citySpinnerSignUpLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:entries="@array/cities"
                app:clicks="@={registerviewmodel.cityOfUser}"
                />

你在 viewModel 中的变量应该看起来像

val cityOfUser = ObservableField<String>().apply {
        addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback(){
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) { 
                Log.d("value",this@apply.get()) //selected value
            }
        })
    }

-------------------------------------------- ----------------------------------

第二种方式比较简单

在 xml

<Spinner
                android:id="@+id/citySpinnerSignUpLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:entries="@array/cities"
              tools:setOnItemSelectedListener="@{registerviewmodel.clicksListener}"
                />

而您的听众是

val clicksListener = object : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            cityOfUser = parent.getItemAtPosition(position) as String
        }
    }

它可能需要一些额外的工作,但一般的方法是这样的。希望对你有帮助。