【问题标题】:Single TextWatcher for multiple EditTexts?多个EditTexts的单个TextWatcher?
【发布时间】:2011-02-27 00:13:56
【问题描述】:

我的活动中有多个 (12) 个 EditText,每对 2 个。当其中的文本发生更改时,我想为每对执行一个操作。我需要有 6 个不同的 TextWatchers,还是有办法将同一个 TextWatchers 用于或进行某种切换?

【问题讨论】:

标签: android android-edittext textwatcher


【解决方案1】:

您可以将相同的TextWatcher 手表附加到每个EditText。根据您需要做什么,您可能需要在某些上下文中创建 TextWatcher 的实现。

【讨论】:

    【解决方案2】:

    我需要这个,所以我做了一个可重复使用的...我扩展了 textwatcher 类,这样我就可以传递我想要观看的视图。

    /**
     *  
     * A TextWatcher which can be reused
     *  
     */
    public class ReusableTextWatcher implements TextWatcher {
    
        private TextView view;
    
        // view represents the view you want to watch. Should inherit from
        // TextView
        private GenericTextWatcher(View view) {
    
            if (view instanceof TextView)
                this.view = (TextView) view;
            else
                throw new ClassCastException(
                        "view must be an instance Of TextView");
        }
    
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i,
                int before, int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int before,
                int count) {
    
            int id = view.getId();
    
            if (id == R.id.someview){
                //do the stuff you need to do for this particular view
    
                }
    
    
    
            if (id == R.id.someotherview){
                //do the stuff you need to do for this other particular view
    
                }
    
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
    
        }
    }
    

    然后要使用它,我会做这样的事情来注册它:

    myEditText.addTextChangedListener(new ReusableTextWatcher(myEditText));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-16
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      相关资源
      最近更新 更多