gpt4 book ai didi

android - onClick 在 android 数据绑定(bind)中的 mvvm 中不起作用

转载 作者:太空狗 更新时间:2023-10-29 16:25:34 28 4
gpt4 key购买 nike

我第一次尝试在 kotlin 中实现 mvvm 和数据绑定(bind)。我遵循了一些教程并且能够实现相同的。但是现在我用 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)

}

}

这是我的登录 Activity

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?

任何按钮点击都不起作用。

最佳答案

我认为,您忘记将 View 模型绑定(bind)到数据绑定(bind)。在 LoginActivity 的 onCreate 中添加:

binding.lifecycleOwner = this
binding.viewmodel = viewModel

关于android - onClick 在 android 数据绑定(bind)中的 mvvm 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57136117/

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