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

时间:2015-02-23 17:38:16

标签: android android-edittext android-custom-view

我能够为动态创建的Edittext添加Textwatcher监听器,所以基本上我会检查第一个editext ontextchanged如果有任何值创建另一个edittext等等。如何检查在监听器中调用哪个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);
    }

}

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,这里使用TextWatcher的目的是在每次填充文本时动态添加另一个EditText。为此,我只需在创建另一个TextWatcher实例时删除它,然后将其添加到新的EditText中。该解决方案使用单个TextWatcher,但与您的请求不同,单个TextWatcher中一次只附加到一个EditText。检查我的示例代码(未经测试)。


编辑下面的编辑代码以删除焦点更改时未使用的EditTexts。不删除文本更改视图的原因是为用户提供更改先前输入的输入而不破坏行的机会。另请注意,最后一行永远不会被删除。根据预期的行数,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);

    }

}