我搜索了很多关于可观察模型和参数的信息,但我很困惑!
1-一些模型从设置器扩展BaseObservable
并设置notifyChange();
在设置器中,并将它们绑定到例如这样的edittext:app:addTextChangedListener="@{viewModel.getEmailTextWatcher}"
并且通过更改模型的参数来notifyChange()
更新文本。email
user
2-其中一些livedata
用于在方法中观察和更改 UI onChanged()
。如何使用liveData
和更改 UI databinding
?不是通过onChanged()
。什么时候onChanged()
使用,而我们可以databinding
用于 UI 更改?
编辑: 这是我的编码:
public class MainActivity extends AppCompatActivity implements LoginResultCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.setViewModel(ViewModelProviders.of(this, new LoginViewModelFactory(this)).get(LoginViewModel.class));
}
@Override
public void onSuccess(String s) {
Toasty.success(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String s) {
Toasty.error(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}
这是界面:
public interface LoginResultCallback {
void onSuccess(String s);
void onError(String s);
}
这是用户模型:
public class User {
@NonNull
public String mEmail;
public User(@NonNull final String email, @NonNull final String password) {
mEmail = email;
}
@NonNull
public String getEmail() {
return mEmail;
}
public void setEmail(@NonNull final String email) {
mEmail = email;
}
}
这是视图模型:
public class LoginViewModel extends ViewModel {
public MutableLiveData<User> user = new MutableLiveData<>();
;
private LoginResultCallback mDataListener;
LoginViewModel(@NonNull final LoginResultCallback loginDataListener) {
mDataListener = loginDataListener;
if (user != null) {
//help me fill here
}
}
public TextWatcher getEmailTextWatcher() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//help me fill here
}
};
}
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com......viewmodels.LoginViewModel" />
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="test"
android:padding="8dp"
android:text="@{viewModel.user}" />
<EditText
android:id="@+id/inEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="8dp"
app:addTextChangedListener="@{viewModel.getEmailTextWatcher}" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="@{viewModel::onLoginClicked}"
android:text="LOGIN" />
</LinearLayout>
</ScrollView>
我只想inEmail
通过数据绑定更改edittext和上部edittext更改