如何检查在动态创建的edittext中调用了哪个edittext textwatcher监听器

How to check which editext textwatcher listener is called in dynamically created edittext

我可以为动态创建的 Edittext 添加 Textwatcher 侦听器,所以基本上我会检查第一个 edittext ontextchanged 是否有任何值创建另一个 edittext,因此 on.How 可以检查在侦听器中调用了哪个 edittext .

我已经检查了几个链接 how-to-use-single-textwatcher-for-multiple-edittexts 但在这种情况下 ID 是固定的。

代码:

 public class MainActivity extends Activity {

    EditText textIn;
    Button buttonAdd;
    LinearLayout container;
    Button buttonShowAll;
    static int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        container = (LinearLayout) findViewById(R.id.container);
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View addView = layoutInflater.inflate(R.layout.row, null);
        final EditText textOut = (EditText) addView.findViewById(R.id.textout);
        textOut.addTextChangedListener(watcher);

        container.addView(addView);

    }

    TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            // addView();

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            /*
             * int childcount = container.getChildCount(); for (int i=0; i <
             * childcount; i++){ View v = container.getChildAt(i);
             * 
             * if(i == (childcount -1)){ Toast.makeText(getApplicationContext(),
             * "Last count"+childcount, Toast.LENGTH_SHORT).show(); addView(); }
             * }
             */
        }
    };

    public void addView() {
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View addView = layoutInflater.inflate(R.layout.row, null);
        final EditText textOut = (EditText) addView.findViewById(R.id.textout);
        textOut.addTextChangedListener(watcher);
        addView.setTag(count++);
        container.addView(addView);
    }

}

如果我没理解错的话,这里使用 TextWatcher 的全部目的是在每次用文本填充前一个 EditText 时动态添加另一个 EditText。为此,我会在创建另一个实例时简单地删除单个 TextWatcher 实例,并将其添加到新的 EditText 中。该解决方案使用单个 TextWatcher,但不同于您的请求,即在单个 TextWatcher 中一次仅附加到一个 EditText。检查我的示例代码(未经测试)。


编辑了下面的 EDIT 代码,以便在焦点更改时也删除未使用的 EditText。不删除 Views on text change 的原因是让用户有机会在不破坏行的情况下更改之前输入的内容。另请注意,最后一行永远不会被删除。根据预期的行数,ListView 可能是更好的选择。

public class MainActivity extends Activity {

    private LinearLayout mContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContainer = (LinearLayout) findViewById(R.id.container);
        ReusableTextWatcher reusableTextWatcher = new ReusableTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) addView(this);
            }

            @Override
            public void onListenerRemoved(final View parentView, final EditText editText) {
                editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus && editText.getText().length() == 0) mContainer.removeView(parentView);
                    }
                });
            }
        };
        addView(reusableTextWatcher);
    }

    private void addView(ReusableTextWatcher reusableTextWatcher) {
        final View addView = LayoutInflater.from(this).inflate(R.layout.row, mContainer);
        reusableTextWatcher.listenTo(addView, R.id.textout);
        mContainer.addView(addView);
    }

    private static abstract class ReusableTextWatcher implements TextWatcher {

        private View mParentView;
        private EditText mCurrentEditText;

        public void listenTo(View parentView, int editTextViewId) {
            if (mCurrentEditText != null) {
                mCurrentEditText.removeTextChangedListener(this);
                onListenerRemoved(mParentView, mCurrentEditText);
            }
            mParentView = parentView;
            mCurrentEditText = (EditText) parentView.findViewById(editTextViewId);
            mCurrentEditText.addTextChangedListener(this);
        }

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

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

        public abstract void onListenerRemoved(View parentView, EditText editText);

    }

}