Я использую привязку данных. Я могу найти информацию о том, что customEditText изменен, понят и использован в другом xml create editTextCustom
cutomEditText. xml:
<?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.smpt.deutschwortschatz.customView.CustomEditTextViewModel" />
<variable
name="view"
type="android.view.View" />
<variable
name="textWacher"
type="android.text.TextWatcher" />
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@{viewModel.textHint}"
>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="@={viewModel.textValue}"
android:layout_gravity="right"
android:gravity="right"
android:textColorHint="@color/colorPrimaryDark"
android:textColorHighlight="@color/colorAccent"
android:backgroundTint="@color/colorPrimaryDark"
/>
/>
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/red"
android:gravity="right"
android:layout_gravity="right"
android:maxLines="1"
android:text="@{viewModel.errorText}"
/>
</LinearLayout>
</layout>
и просмотреть модель Код здесь:
package com.smpt.deutschwortschatz.customView;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Observable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import androidx.databinding.BaseObservable;
import androidx.databinding.ObservableField;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.smpt.deutschwortschatz.ObservableValues;
import com.smpt.deutschwortschatz.R;
import java.text.AttributedCharacterIterator;
public class CustomEditTextViewModel extends ViewModel {
ObservableField<String> textValue = new ObservableField<>("");
ObservableField<String> textHint = new ObservableField<>("");
MutableLiveData<String> errorText = new MutableLiveData<>();
Context context;
AttributeSet attribute;
TypedArray valueOfTextHint;
OnTextValueChanged onTextValueChanged;
public interface OnTextValueChanged {
void onText(String text);
}
public void setOnTextValueChanged(OnTextValueChanged onTextValueChanged) {
this.onTextValueChanged = onTextValueChanged;
}
public void init(Context context, AttributeSet attr) {
this.context = context;
this.attribute = attr;
valueOfTextHint = context.obtainStyledAttributes(attribute, R.styleable.CustomEditText, 0, 0);
textHint.set(valueOfTextHint.getString(R.styleable.CustomEditText_textHint));
valueOfTextHint.recycle();
}
public String getTextHint() {
return textHint.get();
}
public ObservableField<String> getTextValue() {
errorText.setValue(valueOfTextHint.getString(R.styleable.CustomEditText_errorText));
return textValue;
}
public void setTextValue(ObservableField<String> textValue) {
this.textValue = textValue;
}
public String textOnChanged(Editable s){
return textValue.get();
}
public void setTextError(){
}
public MutableLiveData<String> getErrorText() {
return errorText;
}
и логин. xml:
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.smpt.deutschwortschatz.login.LoginViewModel" />
<variable
name="customEditTextViewModel"
type="com.smpt.deutschwortschatz.customView.CustomEditTextViewModel" />
<variable
name="view"
type="android.view.View" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".login.LoginActivity">
<com.smpt.deutschwortschatz.customView.CustomEditText
android:id="@+id/phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
app:textHint="@string/phone_number"
android:layout_marginRight="@dimen/margin_right"
android:layout_marginLeft="@dimen/margin_left"
android:inputType="number"
app:errorText="@{customEditTextViewModel.textValue}"
/>
<com.smpt.deutschwortschatz.customView.CustomButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="@string/app_name"
android:layout_alignParentBottom="true"
android:layout_margin="@dimen/margin_top"
android:onClick="@{(view)->viewModel.continueLogin(view,phoneNumber)}"
/>
</RelativeLayout>
</layout>
Я хочу, чтобы textValue onChanged всегда понимал при входе в систему. xml, потому что, используя setError Text при editTextChanged спасибо