清理此代码的建议

时间:2018-06-28 02:01:31

标签: encapsulation textwatcher code-cleanup

我是编码的新手,我觉得这写得不好。如何减少此代码的重复性?有没有更好的方法通过editText字段将数据设置到模型对象?

 mVarietyField = v.findViewById(R.id.varietyInput);
    mVarietyField.addTextChangedListener(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) {

            mMalt.setVariety(s.toString());

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    mColorField = v.findViewById(R.id.colorInput);
    mColorField.addTextChangedListener(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) {

            mMalt.setColor(Double.parseDouble(s.toString()));

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

0 个答案:

没有答案