I am using the data binding library and i got it so for but there are issues that i cant't seem to get.For starters
This is a part of my xml layout activity_login.xml
<data>
<variable
name="viewModel"
type="com.ViewModel.LoginViewModel" />
</data>
<EditText
android:id="@+id/edit_text_username_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"
android:imeOptions="actionNext"
android:inputType="text"
android:textSize="15sp"
android:theme="@style/EditTextTheme"
app:addTextChangedListener="@{viewModel.getUsername}" />
And my ViewModel contains
public TextWatcher getUsername() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i("username", s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
The above code is to fetch the username as the user types.Is that the correct way of using data binding or am i doing it wrong?The above code works perfectly. The problem is when i try to do it the way i found it online that is by adding
android:addTextChangedListener="@{viewModel.getUsername}"
it does not work and i get a warning saying unknown attribute and if i try compiling it i get a databinding does not exist error.I am also using a radiogroup containing 2 radio buttons and i would like to fetch which one was selected using databinding.I found online sources which use android:onCheckedChanged which i tried using but i get an error again saying binding does not exist and unknown attribute
InverseBindingMethod
is a good stuff here, follow an example.
class
public class Handlers {
public void onKeywordChanged(CharSequence s, int start, int before, int count) {
// Your code here
}
}
layout
<layout>
<data>
<variable name="handlers"
type="my.package.Handlers" />
</data>
...
<EditText
...
android:onTextChanged="@{handlers::onKeywordChanged}"
/>
</layout>
For more information https://developer.android.com/reference/android/databinding/InverseBindingMethod.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.