多个EditText侦听器

时间:2014-08-15 20:17:50

标签: android android-edittext listener

我有3个 EditText的(一个用于长度,另一个用于宽度,另一个用于高度),目标是:

当编辑所有三个 editTexts 时,会自动使用结果更新一个 textField (结果是带有这三个数字的等式)。

我用一个按钮做了这个,当我点击按钮时,他检查3个文字的值,并在结果TextField上设置文本。像这样:

private void Calculator(View view){
        EditText length = (EditText)view.findViewById(R.id.length);
        EditText width = (EditText)view.findViewById(R.id.width);
        EditText height = (EditText)view.findViewById(R.id.height);

        Button button = (Button)view.findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int calc = calculator(length.getText(),width.getText(),height.getText());
                TextView result = (TextView)view.findViewById(R.id.result);
                result.setText(calc);
            }
        });
    }

}

但我不想那样,我想要动态更新。

2 个答案:

答案 0 :(得分:0)

将常见的TextChangeListener添加到所有三个editTexts,并在每次输入任何文本时检查是否所有editText字段都有一些值。

MyTextWatcher mTextWatcher=new TextWatcher() {

   public void afterTextChanged(Editable s) {
   }

   public void beforeTextChanged(CharSequence s, int start, 
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start, 
     int before, int count) {
   //check whether all the textviews have value
   }
  });

 myTextBox1.addTextChangedListener(mTextWatcher);
 myTextBox2.addTextChangedListener(mTextWatcher);
 myTextBox3.addTextChangedListener(mTextWatcher);

答案 1 :(得分:0)

试试这个

import android.widget.EditText;

import java.util.regex.Pattern;

public class Validation {

    // Regular Expression
    // you can change the expression based on your need
    private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    private static final String PHONE_REGEX = "\\d{3}-\\d{7}";
    private static final String COUNTRY_STATE_REGEX = "^[a-zA-Z]{2}$";
    private static final String NAME_REGEX = ".{2,}";
    private static final String NUMERIC_REGEX = "[0-9]+";

    // Error Messages
    private static final String REQUIRED_MSG = "required";
    private static final String EMAIL_MSG = "invalid email";
    private static final String PHONE_MSG = "###-#######";
    private static final String NAME_MSG = "min 2 char";
    private static final String NUMERIC_MSG = "invalid number";

    // call this method when you need to check email validation
    public static boolean isEmailAddress(EditText editText, boolean required) {
        return isValid(editText, EMAIL_REGEX, EMAIL_MSG, required);
    }

    // call this method when you need to check phone number validation
    public static boolean isPhoneNumber(EditText editText, boolean required) {
        return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
    }

    public static boolean isName(EditText editText, boolean required) {
        return isValid(editText, NAME_REGEX, NAME_MSG, required);
    }

    public static boolean isNumeric(EditText editText, boolean required) {
        return isValid(editText, NUMERIC_REGEX, NAME_MSG, required);
    }

    // return true if the input field is valid, based on the parameter passed
    public static boolean isValid(EditText editText, String regex, String errMsg, boolean required) {

        String text = editText.getText().toString().trim();
        // clearing the error, if it was previously set by some other values
        editText.setError(null);

        // text required and
        if (required) {
            //editText is blank, so return false
            if (!hasText(editText)) {
                return false;
            }

            // pattern doesn't match so returning false
            if (!Pattern.matches(regex, text)) {
                editText.setError(errMsg);
                return false;
            }
        }

        return true;
    }

    // check the input field has any text or not
    // return true if it contains text otherwise false
    public static boolean hasText(EditText editText) {

        String text = editText.getText().toString().trim();
        editText.setError(null);

        // length 0 means there is no text
        if (text.length() == 0) {
            editText.setError(REQUIRED_MSG);
            return false;
        }

        return true;
    }
}

在您的活动中

private void initListener() {
    TextWatcher tw = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            if (validate()) {
                // call your function for update...
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            // TODO Auto-generated method stub
        }

    };

    firstEditText.addTextChangedListener(tw);
    secondEditText.addTextChangedListener(tw);
    otherEditText.addTextChangedListener(tw);
}

private boolean validate() {

    return Validation.isNumeric(firstEditText, true)
            && Validation.isNumeric(secondEditText, true)
            && Validation.isNumeric(otherEditText, true);
}