vendredi 4 janvier 2019

How to have reference of UI component in the ViewModel layer

I am learning how to develop using MVVM. I created a small example as shown below in the code. My question is, how can I have a reference to the whole UI component such as EditText, Button in the ViewModel class? the reason why I am attempting to do so is, I want to change the hint added to the EditText programmatically , and to do so I have to call sethint method on an reference of the UI component EditText.

I asked a question before about how to bind android:hint to callback in the ViewModel class, so that when the EditText is initialized, the value of the android:hint will be retrieved from the callback in the ViewModel.

But now the question is different, I want to have access to a reference of EditText so i can call call .sethint programmatically

xml

        android:id="@+id/actMain_LoginViewModel1_et_userId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@{LoginViewModel1.onHintChanged()}"
        android:afterTextChanged="@{(editable)->LoginViewModel1.onAfterUserIdChanged(editable)}"

viewmodel

public class LoginViewModel1 extends BaseObservable {
private final static String TAG = LoginViewModel1.class.getSimpleName();

public String onHintChanged(CharSequence chars) {
   Log.d(TAG, "onHintChanged chars: " + chars);
   return "HINT....";
}
}

Aucun commentaire:

Enregistrer un commentaire