【问题标题】:Data Binding API How to set a String数据绑定 API 如何设置字符串
【发布时间】:2015-11-30 14:24:59
【问题描述】:

当我在 android 中使用 Data Binding API 时,我想删除像 findViewById 这样的样板代码。

但是我有一个简单的 login_fragment,它需要一个 user-name 和一个 user-pw。点击Sign Up后应该会得到相应的字符串并写入我的User-Class

public class User extends BaseObservable {
    private String user;
    private String pw;

    public User() {}

    @Bindable
    public String getUser() {
        return user;
    }

    @Bindable
    public String getPw() {
        return pw;
    }

    public void setUser(String user) {
        this.user = user;
        notifyPropertyChanged(com.example.myuser.lala.BR.user);
    }

    public void setPw(String pw) {
        this.pw = pw;
        notifyPropertyChanged(com.example.myuser.lala.BR.pw);
    }
}

我的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data class="LoginData">
        <variable
            name="logindata"
            type="com.example.myname.lala.User" />
    </data>
<RelativeLayout
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

     <TextView
        android:text="@string/user_name"
        android:id="@+id/textView"
        style="@style/textview_user" />

    <EditText
        android:hint="@string/enter_name"
        android:id="@+id/editText2"
        android:text="@{logindata.user}"
        style="@style/edittext_user" />

    <TextView
        android:text="@string/password"
        android:id="@+id/textView2"
        style="@style/textview_pw" />

    <EditText
        android:id="@+id/editText3"
        android:hint="@string/enter_password"
        android:text="@{logindata.pw}"
        style="@style/edittext_pw" />

    <Button
        android:text="@string/login"
        android:id="@+id/button2"
        style="@style/login_button" />
</RelativeLayout>
</layout>

我将xml-layout 中的变量绑定到用户界面:

LoginData logindata = DataBindingUtil.bind(getView());
logindata.setLogindata(new User());

然后在我输入用户名和密码后,我点击注册,我希望数据绑定通过setUsersetPw 自动更新User-Class 中的userpw 但是当我尝试检索用户或密码:

Log.e("user","" + logindata.getLogindata.getUser()); //output: user null
Log.e("pw","" + logindata.getLogindata.getPw()); //ditto

也许我误解了ViewModel 的用途,比如我的User-Class...

【问题讨论】:

    标签: android data-binding android-databinding


    【解决方案1】:

    如今,Android 中的绑定是“一种方式”。如果您需要 EditText 的值,请使用 watcher。

    添加app:addTextChangedListener属性

            <EditText
                ....
                android:text="@{viewModel.loginText}"
                app:addTextChangedListener="@{viewModel.loginWatcher}" />
    

    以及相应的观察者getter:

    public TextWatcher getLoginWatcher() {
        return new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                loginText = charSequence.toString();
            }
        };
    }
    

    【讨论】:

    • 谢谢 tse.. 但我的 login.xml 中总是有data binding error - could not find accessor com.example.user.lala.User.loginwatcher?你有什么想法吗?
    • 你能用视图显示观察者的定义和部分xml吗?
    【解决方案2】:

    您需要实现双向数据绑定才能从视图/控件中获取值。简单在@后提供=符号实现双向绑定

    android:text="@={logindata.user}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      相关资源
      最近更新 更多