[英]How to develop a library and an application side-by-side in Android Studio?
[英]How to develop application with Android Data Binding Library and MVVM?
我已經使用了Android數據綁定庫,MVVM和翻新版。
有兩個字段,分別稱為“雇員身份”和“密碼”。 當我單擊登錄按鈕時,我需要這些值。 我在LoginViewModel中編寫了Button事件。 當我單擊登錄按鈕時,我得到空值。 我能做什么?
如果我對數據綁定庫和MVVM錯誤。 你能提一下嗎?
public void onClickLogin(View view) {
LoginViewModel loginViewModel=new LoginViewModel(mContext);
String EmpId=loginViewModel.getEmployeeid();
String Pass=loginViewModel.getPassword();
Toast.makeText(mContext, "EmployeeID is:"+EmpId+"Password is:"+Pass, Toast.LENGTH_LONG).show();
}
型號-登錄:
public class Login {
@SerializedName("EmpID")
private String employeeid;
@SerializedName("FullName")
private String fullname;
@SerializedName("UserType")
private String usertype;
String password;
public Login(String employeeid, String fullname, String usertype, String password) {
this.employeeid = employeeid;
this.fullname = fullname;
this.usertype = usertype;
this.password = password;
}
public String getEmployeeid() {
return employeeid;
}
public void setEmployeeid(String employeeid) {
this.employeeid = employeeid;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getUsertype() {
return usertype;
}
public void setUsertype(String usertype) {
this.usertype = usertype;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
LoginViewModel:
public class LoginViewModel extends BaseObservable {
private Login mLogin;
private String employeeid;
private String password;
private String fullname;
private Context mContext;
String TAG= LoginActivity.class.getSimpleName();
public LoginViewModel(Context mContext) {
this.mContext = mContext;
}
@Bindable
public String getEmployeeid() {
return employeeid;
}
public void setEmployeeid(String employeeid) {
this.employeeid = employeeid;
notifyPropertyChanged(BR.employeeid);
}
@Bindable
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
notifyPropertyChanged(BR.password);
}
//@Bindable
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
//notifyPropertyChanged(BR.fullName);
}
public void onClickLogin(View view) {
LoginViewModel loginViewModel=new LoginViewModel(mContext);
String EmpId=loginViewModel.getEmployeeid();
String Pass=loginViewModel.getPassword();
Toast.makeText(mContext, "EmployeeID is:"+EmpId+"Password is:"+Pass, Toast.LENGTH_LONG).show();
}
}
LoginActivity:
public class LoginActivity extends AppCompatActivity {
Login gg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_login);
ActivityLoginBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_login);
LoginViewModel lvm=new LoginViewModel(this);
//lvm.setFullName("Durlove Roy");
binding.setLoginVM(lvm);
binding.setLoginActivity(this);
}
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login">
<data>
<variable
name="loginVM"
type="com.nitolniloygroup.operating.viewmodel.LoginViewModel"></variable>
<variable
name="loginActivity"
type="com.nitolniloygroup.operating.view.activity.LoginActivity"></variable>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="@={loginVM.employeeid}"
android:ems="10"
android:id="@+id/editText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="@={loginVM.password}"
android:ems="10"
android:id="@+id/editText2" />
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{loginVM::onClickLogin}"
android:id="@+id/button" />
</LinearLayout>
</layout>
問題在這里:
public void onClickLogin(View view) {
LoginViewModel loginViewModel=new LoginViewModel(mContext);
String EmpId=loginViewModel.getEmployeeid();
String Pass=loginViewModel.getPassword();
Toast.makeText(mContext, "EmployeeID is:"+EmpId+"Password is:"+Pass, Toast.LENGTH_LONG).show();
}
您將實例化一個新的LoginViewModel而不是使用現有的LoginViewModel。 改為這樣做:
public void onClickLogin(View view) {
Toast.makeText(mContext, "EmployeeID is:" + this.employeeid
+ " Password is:" + this.password, Toast.LENGTH_LONG).show();
}
或者,您可以將它們作為參數傳遞到綁定表達式中。 如果按鈕事件處理程序與視圖模型位於不同的類中,則這可能很有用:
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{() -> loginVM.onClickLogin(loginVM.employeeid, loginVM.password)}"
android:id="@+id/button" />
點擊處理程序將如下所示:
public void onClickLogin(String empId, String pwd) {
Toast.makeText(mContext, "EmployeeID is:" + empId
+ " Password is:" + pwd, Toast.LENGTH_LONG).show();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.