我有一个简单的登录表单,用户可以在其中输入用户名和密码,然后点击登录按钮。
我有我的布局文件,类似于这样的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 中,但是当用户输入实际的用户名和密码时,它不会采用用户输入的新值。
那么如何获取用户输入的文本呢?