其他分享
首页 > 其他分享> > 尝试在Android中的对话框中实现数据绑定

尝试在Android中的对话框中实现数据绑定

作者:互联网

我正在尝试在我的android应用程序中实现数据绑定.
我有一个对话框作为我的登录名,它位于我的主要活动something like this

这是我的主要活动

public class MapsActivity extends FragmentActivity {

    LayoutUserAuthBinding layoutBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        UserAuthDialogSettings dialogSettings = new UserAuthDialogSettings(this);

        Dialog dialog = dialogSettings.dialogSettings();
        dialog.show();

        layoutBinder = DataBindingUtil.inflate(LayoutInflater.from(dialog.getContext()),R.layout.layout_user_auth,null,false);
        layoutBinder.setMain(this);
    }

    public void onLoginButtonClick()
    {
        UserAuthModel model = layoutBinder.getUserAuth();
        Log.e("MAIN","NAME : " + model.getUserName());
        Log.e("MAIN","PASSWORD" + model.getUserPassword());
        Log.e("MAIN","PASSPHRASE" + model.getUserPassphrase());

        layoutBinder.getUserAuth().setUserName("shantanu");
        layoutBinder.getUserAuth().setUserPassword("password123");
        layoutBinder.getUserAuth().setUserPassphrase("passphrase123");
    }
}

这是我的模特班

public class UserAuthModel extends BaseObservable{
@NonNull
String userName,userPassword,userPassphrase;

public UserAuthModel(String userName, String userPassword, String userPassphrase) {
    this.userName = userName;
    this.userPassword = userPassword;
    this.userPassphrase = userPassphrase;
}

@Bindable
public String getUserName() {
    return userName;
}

@Bindable
public String getUserPassword() {
    return userPassword;
}

@Bindable
public String getUserPassphrase() {
    return userPassphrase;
}

public void setUserName(@NonNull String userName) {
    this.userName = userName;
    notifyPropertyChanged(BR.userName);
}

public void setUserPassword(@NonNull String userPassword) {
    this.userPassword = userPassword;
    notifyPropertyChanged(BR.userPassword);
}

public void setUserPassphrase(@NonNull String userPassphrase) {
    this.userPassphrase = userPassphrase;
    notifyPropertyChanged(BR.userPassphrase);
}
}

这是我的对话框布局

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

    <variable
        name="UserAuth"
        type="model.UserAuthModel" />

    <variable
        name="main"
        type="com.plotalong.android.MapsActivity" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="User Name"
        android:inputType="textPersonName"
        android:text="@={UserAuth.userName}" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        android:text="@={UserAuth.userPassword}" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Pass Phrase"
        android:inputType="textPassword"
        android:text="@={UserAuth.userPassphrase}" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="right|center_vertical">

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{() -> main.onLoginButtonClick()}"
            android:text="Submit" />
    </TableRow>
</LinearLayout>
</layout>

按钮的onClick()不会做任何事情,我正在尝试从按钮单击事件的editText中获取文本

解决方法:

您的Dialog和layoutBinder之间没有链接,应该将其绑定.您可以使用dialog.setContentView(layoutBinder .getRoot());执行该操作.

Dialog dialog = dialogSettings.dialogSettings();

layoutBinder = DataBindingUtil.inflate(LayoutInflater.from(dialog.getContext()),R.layout.layout_user_auth,null,false);
layoutBinder.setMain(this);
dialog.setContentView(layoutBinder .getRoot());
dialog.show();

标签:android-databinding,2-way-object-databinding,android,mvvm
来源: https://codeday.me/bug/20191026/1932806.html