Android 运行时TextWatcher控件

Android 运行时TextWatcher控件,android,textwatcher,Android,Textwatcher,我有4个edittext,我想用一个控件值实现一个TextWatcher Et1Burro.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable value) { // you can call or do what you want with your EditText here Dvalue = Get

我有4个edittext,我想用一个控件值实现一个TextWatcher

Et1Burro.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable value) {
              // you can call or do what you want with your EditText here
                Dvalue = GetEditValue(value);
                double et4tot = 0, et2fibra = 0, et3zucc = 0;
                // et1burro + et2fibra = et4tot
                // et1burro + et2fibra + et3zucc = 100
                try {
                    et4tot = Double.parseDouble(Et4Tot.getText().toString());
                } catch (NumberFormatException e) { e.printStackTrace(); }

                try {
                    et2fibra = Double.parseDouble(Et2Fibra.getText().toString()); 
                } catch (NumberFormatException e) { e.printStackTrace(); }

                try {   
                    et3zucc = Double.parseDouble(Et3Zucc.getText().toString());
                } catch (NumberFormatException e) { e.printStackTrace(); }

                if ((Dvalue < 1) || (Dvalue > 100) || ((Dvalue + et2fibra) != et4tot ) || ((Dvalue + et2fibra + et3zucc) != 100 ))
                {
                //segnala errore
                    Et1Burro.setTextColor(getActivity().getBaseContext().getResources().getColor(R.color.Red)); 
                }else
                    Et1Burro.setTextColor(getActivity().getBaseContext().getResources().getColor(R.color.Black));
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
         });    
正确的示例值为A=35、B=35、C=30、D=70 但是,如果在第一次编辑(A)中,用户插入第一个字符ex35,程序将以红色值响应,因为其他值为0,当用户编译所有编辑文本时,值为35,35,30,70的黑色anly值是最后一个。
我希望澄清…

因为您只更新了一个编辑文本的颜色

我的建议是

  • 将所有编辑文本的默认颜色设置为红色
  • 为“控制值”逻辑编写一个函数。仅当所有编辑文本中都有值时才调用此函数
  • 如果输入的值通过控制逻辑,则将editbox的所有颜色更新为黑色。否则你必须把它设成红色

TextWatcher
应该是一条出路。您是否担心在回调中做得太多了?
TextWatcher
如果您只是简单地更改文本颜色,应该可以。现在,您的实际问题是什么?setTextColor正在查找int。您可以使用setTextColor(Color.rgb(int-red,int-green,int-blue))或setTextColor(Color.red/Color.BLACK)。
A+B = D
A+B+C = 100
C = 100 - D