如何将TextWatcher添加到多个EditTexts

时间:2014-03-30 19:26:34

标签: java android textwatcher

我已经查看了其他一些问题和帖子,但我似乎无法解决这个问题......我正在建立一个转换应用程序,我可以通过将英寸放入,自动填充英尺和码,使用addTextChangedListener,但我无法弄清楚如何将textTextChangedListener添加到其他字段,以便我可以输入英尺并自动转换为英寸和码,以及码到英寸和英尺......

   @Override
protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       final EditText editIn = (EditText) findViewById(R.id.editIn);
       final EditText editFt = (EditText) findViewById(R.id.editFt);
       final EditText editYd = (EditText) findViewById(R.id.editYd);

       editIn.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
           @Override
           public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
               try {
                   double in = Double.valueOf(editIn.getText().toString());
                   double ft = in / 12;
                   double yd = in / 36;
                   editFt.setText(String.valueOf(ft));
                   editYd.setText(String.valueOf(yd));
               } catch (NumberFormatException e) {
                   e.printStackTrace();
               }
           }
           @Override
           public void afterTextChanged(Editable editable) { }

2 个答案:

答案 0 :(得分:1)

    TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //YOUR CODE
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //YOUR CODE
        }

        @Override
        public void afterTextChanged(Editable s) {

                //YOUR CODE
        }
    };

    editIn.addTextChangedListener(watcher);
    editFt.addTextChangedListener(watcher);
    editYd.addTextChangedListener(watcher);

答案 1 :(得分:0)

嘿,因为你问了不同的方法....我想出了一种方法来使用textwatcher来验证不同的字段,每个字段都可以有自己的验证。

/* A TextWatcher which can be reused anonymously only.
 *  
 */
public abstract class MyTextWatcher implements TextWatcher {

    private TextView view;


    // view represents the view you want to watch. Should inherit from
    // TextView
    private MyTextWatcher(View view) {

        if (view instanceof TextView){
            this.view = (TextView) view;
        }
        else
            throw new ClassCastException(
                    "view must be an instance Of TextView");
    }

    /**
     * 
     * @param view - the textview to validate
     * override this to handle any textview validations and UI error handling
     */

     public abstract void validate(TextView view);



    @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) {
        validate(); //this calls all the different views validate() methods

    }

    @Override
    public void afterTextChanged(Editable editable) {           
    }

    public void forceValidation(){
        validate(view);

    }

    public void markAsErrorField(){
         if(isEmptyField()){

             view.setHintTextColor(getResources().getColorStateList(R.color.red));

         }
    }

    //resets textviews to default appearance
    public void markAsDefault(){
             view.setHintTextColor(getResources().getColorStateList(R.color.default));
    }


    public boolean isEmptyField() {
        return view.getText().toString().trim().equals("");
    }

}

之后,我可以匿名使用上述方法来设置我想要运行的不同验证方法。在上面我有markAsDefault和markAsErrorField方法......下面是如何应用它们:

et1.addTextChangedListener(new MyTextWatcher(et1){
        @Override
        public void validate(TextView view) {

            markAsErrorField();
        }
    });

et2.addTextChangedListener(new MyTextWatcher(et2){
            @Override
            public void validate(TextView view) {

                markAsDefault();
            }
        });

作为额外的:如果你想使用forceValidate方法,那么在外部类和textwatcher构造函数中使用静态arraylist,将每个实例保存到它。然后有for循环,为每个textwatcher存储调用forceValidate();