繁体   English   中英

(MVC)在复合视图上实现TextWatcher

[英](MVC) Implement TextWatcher on Compound view

我是MVC的新手,我正尝试为学校创建一个颜色选择器Android应用程序。 目的是创建多个复合视图以显示相同的数据(红色,绿色和蓝色值)。 我的问题是我应该如何使用TextWatcher来确定何时更改了EditText字段,然后根据该字段的值更新所有视图。 到目前为止,这是我的代码:

ColorModel.java

public class ColorModel extends Observable {
    private int red, green, blue;

    public void updatedColorModel() {
        this.setChanged();
        this.notifyObservers();
    }

    /**
     * Change the value of the RED color
     * @param value
     */
    public void setRed(int value) {
        this.red = value;
        updatedColorModel();
    }

    /**
     * Change the value of the GREEN color
     * @param value
     */
    public void setGreen(int value) {
        this.green = value;
        updatedColorModel();
    }

    /**
     * Change the value of the BLUE color
     * @param value
     */
    public void setBlue(int value) {
        this.blue = value;
        updatedColorModel();
    }

    public int getRed() {
        return this.red;
    }

    public int getGreen() {
        return this.green;
    }

    public int getBlue() {
        return this.blue;
    }
}

ETColorsCOMP.java (3个EditText的复合)

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());
    }
}

VColorsCOMP.java (3个视图组件的复合)

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()));
    }
}

MainActivity.java

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);
    }
}

如果有人帮助我完成这项工作,我将不胜感激。

您可以为此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
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM