我正在玩android,这并不是一个真正的目的,它只是发现的东西。Android MVVM与数据绑定
因此,我有一个textWatcher和2个editText框,当用户更改文本时,如何更改文本的颜色?
例如,假设用户输入的不是字母,则文本必须是红色。
对于性能问题(与此有关吗?)我想只有一个textWatcher表单中的所有字段需要相同的验证规则(我在网上找到的每个教程每个字段有1个观察者,也许在那里是这样的原因)
起初,我想使用android:addTextChangedListener=
,但这不会将edittext对象发送给观察者,所以我知道'一个字段'已经更改,但不是哪一个。
我有这样的:
视图(3场,2文本和1个数字)
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="basicForm"
type="com.example.mvvm.databinding.BasicForm" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/constraint"
tools:context="com.example.mvvm.databinding.MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<EditText
android:id="@+id/nameField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="241dp"
android:ems="10"
android:hint="@{basicForm.fieldName}"
android:inputType="textPersonName"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/lastnameField" />
<EditText
android:id="@+id/lastnameField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:textColor="#00FF00"
android:hint="@{basicForm.fieldLastName}"
android:inputType="textPersonName"
android:layout_marginBottom="81dp"
android:layout_alignBottom="@+id/nameField"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/phoneField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/nameField"
android:layout_below="@+id/nameField"
android:layout_marginTop="35dp"
android:ems="10"
android:hint="@{basicForm.fieldPhone}"
android:inputType="textPersonName" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
</layout>
MainActivity:
package com.example.mvvm.databinding;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import com.example.mvvm.databinding.databinding.ActivityMainBinding;
public class MainActivity extends FormActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
final BasicForm bf = new BasicForm();
bf.setFieldPhone("06XXXXXXXX");
bf.setFieldName("Name");
bf.setFieldLastName("Last Name");
bf.setStringWatcher(getStringWatcherInstance());
bf.setPhoneWatcher(getPhoneWatcherInstance());
binding.setBasicForm(bf);
}
}
父主要活动的:
package com.example.mvvm.databinding;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import java.util.regex.Pattern;
public class FormActivity extends AppCompatActivity {
private static TextWatcher stringWatcherInstance = null;
private static TextWatcher phoneWatcherInstance = null;
public boolean stringContainsOnlyChar(String s) {
if(! Pattern.matches(".*[a-zA-Z]+.*[a-zA-Z]", s)) {
return true;
}
return false;
}
public synchronized TextWatcher getStringWatcherInstance() {
if(stringWatcherInstance == null) {
stringWatcherInstance = 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) {
if (stringContainsOnlyChar(s.toString())) {
//set color of calling edit box to black
}
//set color of calling edif box to red
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
return stringWatcherInstance;
}
public synchronized TextWatcher getPhoneWatcherInstance() {
if(phoneWatcherInstance == null) {
phoneWatcherInstance = 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) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
return phoneWatcherInstance;
}
}
和BasicForm obj ECT数据绑定:
package com.example.mvvm.databinding;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.text.TextWatcher;
public class BasicForm extends BaseObservable {
private String fieldName;
private String fieldLastName;
private String fieldPhone;
public BasicForm() {}
@Bindable
public String getFieldName() {
return fieldName;
}
public void setFieldName(String FieldName) {
this.fieldName = FieldName;
notifyPropertyChanged(BR.fieldName);
}
@Bindable
public String getFieldLastName() {
return fieldLastName;
}
public void setFieldLastName(String fieldLastName) {
this.fieldLastName = fieldLastName;
}
@Bindable
public String getFieldPhone() {
return fieldPhone;
}
public void setFieldPhone(String fieldPhone) {
this.fieldPhone = fieldPhone;
notifyPropertyChanged(BR.fieldPhone);
}
}
所以基本上我想要的只是一个“charOnlyTextWatcher”我可以在XML,然后在它的onChange关联()方法可以在调用者采取行动(触发的onChange中的EditText对象())
我在做一些无用的事吗?错误?
谢谢。