我正在尝试在我的Android应用程序中实现数据绑定。 我有一个Dialog作为我的登录信息,它位于我的主要活动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中获取文本
答案 0 :(得分:4)
您的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();