我有一个简单的logIn表单,用户可以输入用户名和密码,点击按钮进入LogIn。
我有我的布局文件,类似于activit_main.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="user"
type="User"></variable>
<variable
name="logInResponse"
type="LogInResponse"></variable>
</data>
<LinearLayout
android:id="@+id/login_section_credentialsBody"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@{user.username}" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@{user.password}" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:onClick="callLogIn"/>
</LinearLayout>
</Layout>
这是我的MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("Log In","Password");
binding.setUser(user);
}
public void callLogIn(View view) {
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
textView.setText("Connecting...");
loginInService.checkLogin(binding.getUser());
}
问题是我第一次在编辑文本中显示登录和密码,并在OnCreate中绑定它,但是当用户输入实际的用户名和密码时,它不会获取用户输入的新值。
那么如何用户输入文字?
根据George和Yigit的Google I / O 2016数据绑定谈话,您可以:
机器人:文字=“@ = {} User.LogIn
在你的xml和两种方式将工作。需要注意的是额外的等号。
您需要在User(viewmodel)中具有可以传递字段数据的功能。例如,下面是我的登录活动的布局文件代码。
<EditText android:layout_width="0dp"
android:id="@+id/email_address_input"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:hint="Email address"
android:gravity="center"
android:layout_height="wrap_content"/>
<EditText android:layout_width="0dp"
android:id="@+id/password_input"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/email_address_input"
android:hint="Password"
android:gravity="center"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:id="@+id/submit"
android:text="Submit"
android:onClick="@{()->viewModel.onClickEmailSignin(emailAddressInput.getText().toString(),passwordInput.getText().toString())}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/password_input"
android:layout_height="wrap_content"/>
这是我的viewmodel函数onClickEmailSignin()
fun onClickEmailSignin(email: String, password: String) {
Log.d(LOG_TAG, "email $email password : $password");
}
目前,android数据绑定是单向通信,因此它不允许将edittext当前文本作为您期望的内容。
我有同样的问题,在阅读官方谷歌文档后,我得到了这个:
MyLayoutBinding binding=DataBindingUtil.setContentView(this,R.layout.my_layout);
现在使用此绑定对象来引用您的视图。假设你有edittext
<EditText
android:id="@+id/edittext_chat_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="@string/hint_typing"
android:layout_marginRight="@dimen/margin_average"
style="@style/regular_editTexts"/>
然后通过binding.edittextChatMessage.getText().toString
参考edittext
难怪数据绑定本身使用其id名称为您创建了edittext引用,与findViewById方法相比,这种获取引用的方式非常快。
你不能。
目前,Android数据绑定只是一种方式。
因此,当您设置android:text =“@ {user.username}”时,会将EditText的文本设置为用户的当前用户名。如果用户或用户名是Observable,那么如果更改,您还将获得更新。
但是,它只是一种方式 - EditText将显示视图模型的更新,但视图模型不会获得EditText的更改。
如果您需要这些更改,则必须手动执行。
这里有两种简单的方法,它取决于您的用例。一,设置TextWatcher并保持名称更新。
另一个,我认为这是你需要的那个,是在callLogIn方法中手动更新它。
另外,请参阅https://medium.com/@fabioCollini/android-data-binding-f9f9d3afc761它有点复杂,但它可能适合您的用例。