我是MVC新手,我正在尝试为学校创建一个Color picker Android应用程序。目的是创建多个复合视图以显示相同的数据(红色,绿色和蓝色值)。我的问题是我应该如何使用TextWatcher来确定EditText字段何时被更改,然后根据字段的值更新所有视图。到目前为止,这是我的代码:
ColorModel.java
public class ETColorsCOMP extends ConstraintLayout implements Observer {
private EditText etRed, etGreen, etBlue;
private ColorModel model;
public ETColorsCOMP(Context context) {
super(context);
init();
}
public ETColorsCOMP(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ETColorsCOMP(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init() {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.edittext_compound, this);
etRed = findViewById(R.id.editTextRed);
etGreen= findViewById(R.id.editTextGreen);
etBlue = findViewById(R.id.editTextBlue);
}
public void setColorModel(ColorModel model) {
this.model = model;
this.model.addObserver(this);
}
@Override
public void update(Observable observable, Object o) {
etRed.setText(model.getRed());
etGreen.setText(model.getGreen());
etBlue.setText(model.getBlue());
}
}
ETColorsCOMP.java (3个EditTexts的复合体)
public class VColorCOMP extends ConstraintLayout implements Observer {
private ColorModel model;
private View vRed, vGreen, vBlue;
public VColorCOMP(Context context) {
super(context);
init();
}
public VColorCOMP(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VColorCOMP(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init() {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.colorview_compound, this);
vRed = findViewById(R.id.ViewRed);
vGreen = findViewById(R.id.ViewGreen);
vBlue = findViewById(R.id.ViewBlue);
}
public void setColorModel(ColorModel model) {
this.model = model;
this.model.addObserver(this);
}
@Override
public void update(Observable observable, Object o) {
vRed.setBackgroundColor(Color.rgb(model.getRed(), 0, 0));
vGreen.setBackgroundColor(Color.rgb(0, model.getGreen(), 0));
vBlue.setBackgroundColor(Color.rgb(0, 0, model.getBlue()));
}
}
VColorsCOMP.java (3个View组件的组合)
public class MainActivity extends AppCompatActivity {
private ETColorsCOMP etColors;
private VColorCOMP vColor;
private ColorModel colorModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etColors = findViewById(R.id.editTextView);
vColor = findViewById(R.id.colorView);
colorModel = new ColorModel();
etColors.setColorModel(colorModel);
vColor.setColorModel(colorModel);
}
}
MainActivity.java
{{1}}
如果有人帮我完成这项工作,我将非常感激。
答案 0 :(得分:0)
您可以在TextWatcher的 afterTextChanged 方法上编写代码。
editText.addTextChangedListener(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) {
// Update UI
}
});