AddTextChangedListener到循环生成的EditText视图

时间:2016-11-16 21:40:21

标签: java android for-loop android-edittext addtextchangedlistener

我是Android开发的新手。我正面临一个为项目开发应用程序的死胡同。请花些时间帮助我。

问题: 我使用EditText生成了LinearLayout嵌套的for loop个视图。

例如:

LinearLayout rootView = (LinearLayout) findViewById(R.id.rootview);
for (int i=0,j=10;i<j;i++) {
    EditText et = new EditText(this);
    rootView.addView(et);
    et.setHint("EditText No. "+ i);
    et.setId(i);
} // This code is for example purposes only.

现在,我似乎无法理解如何在特定时间聚焦的EditText上设置addTextChangedListener,并在其他EditTexts上设置该文本。请告诉我应采取什么方法来实现这一目标。我尽力解释这个问题;但是,如果仍有任何歧义,请随时发表评论并提出要求。我正在等待解决这个问题。

就截图而言:

我有什么:

enter image description here

我想要的是什么:

enter image description here

我希望这能解决问题!

**

编辑:

感谢TylerSebastian的解决方案。我得到了它的工作。这是最终的代码:( Inside OnCreate()方法)

final LinearLayout rootView = (LinearLayout) findViewById(R.id.rootview);
    for (int i=0,j=10;i<j;i++) {

        final EditText et = new EditText(this);
        rootView.addView(et);
        et.setHint("EditText No. "+ i);
        et.setId(i);
        final TextWatcher textwatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

            }

            @Override
            public void afterTextChanged(final Editable s) {
                for (int i=0;i<10;i++){
                    EditText view = (EditText) findViewById(i);
                    if (view != et){
                        view.setText(s.toString());
                    }
                }
            }
        };
        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    et.addTextChangedListener(textwatcher);
                } else {
                    et.removeTextChangedListener(textwatcher);
                }
            }
        });

    } 

**

2 个答案:

答案 0 :(得分:2)

我无法访问带有AS的计算机,因此不能保证以下内容没有错误,但这应该指向正确的方向:

final LinearLayout rootLayout = ...;

// within your loop
et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

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

    @Override
    public void afterTextChanged(final Editable s) {
        for (int i = 0; i < rootLayout.getChildCount(); i++) {
            View view = rootLayout.getChildAt(i);

            if (view instanceof EditText && view != et) {
                ((EditText) view).setText(s.toString());
            }
        }
    }
});

修改:所以上面会导致无限循环 - 请参阅下面的评论

怎么样:

final LinearLayout rootLayout = ...;

// again, within your loop
TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

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

    @Override
    public void afterTextChanged(final Editable s) {
        for (int i = 0; i < rootLayout.getChildCount(); i++) {
            View view = rootLayout.getChildAt(i);

            if (view instanceof EditText && view != et) {
                ((EditText) view).setText(s.toString());
            }
        }
    }
};

et.setOnFocusChangeListener(new OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
             ((EditText) view).addTextChangedListener(textWatcher);
        } else {
             ((EditText) view).removeTextChangedListener(textWatcher);
        }
    }
});

基本上,只有焦点元素才会有textwatcher

答案 1 :(得分:-1)

首先在XML中添加一个LinearLayout

<LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

然后创建一个新类

public class EditTextCust extends RelativeLayout  {

EditText status;

public EditTextCust(Context context, Model post, int inputType,TextWatcher textWatcher) {
    super(context);
    inflate(context, R.layout.edit_text_cust, this);
    status = (EditText) findViewById(R.id.editText);
    status.setInputType(inputType);
    post.setData(status.getText().toString());
    status.setHint(post.getHint());
    status.addTextChangedListener(textWatcher);
}

public String getText() {


    return status.getText().toString();
}

public EditTextCust(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public EditTextCust(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}
}

为此自定义视图类创建新的XML文件名edit_text_cust

            <EditText
                android:id="@+id/dateEditText"
                android:layout_width="match_parent"
                android:background="@android:color/transparent"
                android:padding="@dimen/_5sdp"
                android:layout_height="match_parent"
                android:textSize="@dimen/_12sdp" /> 

和creat模型类来设置数据

public class Model {

String hint;
String id;
String data;

public String getHint() {
    return hint;
}

public void setHint(String hint) {
    this.hint = hint;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}
}

并在for循环中的类中,但此代码

 Model model = new Model();
            model.setHint("HINT");
            model.setId("1");

            editTextCust = new EditTextCust(this, editTextCustModel, InputType.TYPE_CLASS_NUMBER, new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // getText from here use
                       s.toString();
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            linearLayout.addView(editTextCust);

它正确地与我合作,如果您对此代码中的任何内容都不了解,请添加评论,我会帮助您:)