【问题标题】:(MVC) Implement TextWatcher on Compound view(MVC) 在复合视图上实现 TextWatcher
【发布时间】:2018-08-20 11:19:24
【问题描述】:

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

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

【问题讨论】:

    标签: java android model-view-controller


    【解决方案1】:

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

    【讨论】:

    • 我应该在我的 ETColorsCOMP.java 文件中还是在活动中使用它?如果它在 Activity 中,这是否意味着我需要为 Compound 的每个 View 使用 findViewById?
    • 我把上面的代码放在 Activity 类的 onCreate() 方法中,效果很好。毕竟,答案很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多