kotlin onClick无法在mvvm中工作在android数据绑定中

e4yzc0pl  于 2023-03-24  发布在  Kotlin
关注(0)|答案(3)|浏览(179)

我第一次尝试在Kotlin中实现mvvm和数据绑定。我遵循了一些教程并能够实现相同的。但现在按钮单击不工作,这是我用mvvm编写的。
下面是activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <variable
            name="viewmodel"
            type="com.abc.abc.presentation.user.viewmodels.LoginViewModel"/>
</data>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".presentation.user.views.LoginActivity"
        android:layout_margin="24dip">
    <Button
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:background="@drawable/btn_user_create_account"
            android:layout_above="@+id/or_layout"
            android:layout_marginBottom="24dp"
            android:text="@string/create_account"
            android:id="@+id/btn_create_account"
            android:layout_marginEnd="12dip"
            android:layout_marginStart="12dip"
            android:gravity="center_horizontal|center_vertical"
            android:textColor="@color/text_color"
            android:textAllCaps="false"
            android:onClick="@{() -> viewmodel.redirectToRegisterActivity()}"
            android:fontFamily="@font/vollkron"
            android:textStyle="bold"
    />
    <Button
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:background="@drawable/btn_user_login_reg"
            android:layout_above="@+id/btn_create_account"
            android:layout_marginBottom="12dp"
            android:text="@string/login"
            android:id="@+id/btn_login"
            android:layout_marginEnd="12dip"
            android:layout_marginStart="12dip"
            android:gravity="center_horizontal|center_vertical"
            android:textColor="@color/white"
            android:textAllCaps="false"
            android:onClick="@{() -> viewmodel.executeEmailLogin(context)}"
            android:fontFamily="@font/vollkron"
            android:textStyle="bold"/>
 </RelativeLayout>

下面是我的ViewModel代码:

class LoginViewModel(application: Application) : AndroidViewModel(application) {

var email: ObservableField<String>? = null
var password: ObservableField<String>? = null
var progressDialog: SingleLiveEvent<Boolean>? = null
var launchRegisterActivity: SingleLiveEvent<Boolean>? = null
var userLogin: LiveData<User>? = null

init {
    progressDialog = SingleLiveEvent<Boolean>()
    launchRegisterActivity = SingleLiveEvent<Boolean>()
    email = ObservableField("")
    password = ObservableField("")
    userLogin = MutableLiveData()
}

fun redirectToRegisterActivity() {
    launchRegisterActivity?.value = true
}

fun executeEmailLogin(context: Context)  {
    progressDialog?.value = true
    val user = User(
        email = email.toString(),
        password = password.toString(),
        )

    userLogin = UserRepository.getInstance().registerUserUsingEmail(context, user)

}

}

这是我的登录活动

class LoginActivity : AppCompatActivity() {

var binding: ActivityLoginBinding? = null
var viewModel: LoginViewModel? = null
var customProgressDialog: CustomProgressDialog? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    FacebookSdk.sdkInitialize(applicationContext);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
    customProgressDialog = CustomProgressDialog(this)
    viewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)
    observeViewModel(viewModel!!)
}

private fun observeViewModel(viewModel: LoginViewModel) {
    viewModel.progressDialog?.observe(this, Observer {
        if (it!!) customProgressDialog?.show() else customProgressDialog?.dismiss()
    })

    viewModel.launchRegisterActivity?.observe(this, Observer {
        if (it!!) startActivity(Intent(this, RegisterActivity::class.java))
    })

    viewModel.userLogin?.observe(this, Observer { user ->
        // redirect user to home scree
        Toast.makeText(this, "welcome, ${user?.user_name}", Toast.LENGTH_LONG).show()
    })

}

}
你能帮助我理解我在这里做错了什么吗?我正确地实现了mvvm吗?
任何按钮点击都不起作用。

7eumitmz

7eumitmz1#

我想,你忘了绑定viewmodel到databinding.添加这个在onCreate的LoginActivity:

binding.lifecycleOwner = this
binding.viewmodel = viewModel
aiazj4mn

aiazj4mn2#

尝试添加

binding.lifecycleOwner = this

在你的布局膨胀之后。

7gcisfzg

7gcisfzg3#

尝试添加:

loginBinding =DataBindingUtil.inflate(inflater, R.layout.fragment_login,container,false)

        viewModelLogin = ViewModelProvider(this, factory).get(ViewModelLogin::class.java)
        loginBinding.mvvmlogin =viewModelLogin

 return loginBinding.root

相关问题