尝试在 Android 的对话框中实现数据绑定
Trying to implement data binding in a Dialog in Android
我正在尝试在我的 android 应用程序中实现数据绑定。
我有一个 Dialog 作为我的登录名,它位于我的主要 activity something like this
这是我的主要activity
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");
}
}
这是我的模型class
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 应用程序中实现数据绑定。 我有一个 Dialog 作为我的登录名,它位于我的主要 activity something like this
这是我的主要activity
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");
}
}
这是我的模型class
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();