2012-01-03 32 views
0

我想创建对象的EditText n个创建的每个对象的EditText ......所以使用LayoutInflater IM做,在它运行至ň...的意见被添加到布局,但环如果我想一个addTextChangeListener添加到每个的EditText对象,听者仅被添加到的最后一个对象...其关闭的问题,我怎么能解决这个问题?添加addTextChangeListener在循环

LinearLayout ll=(LinearLayout) findViewById(R.id.i1); 
    LayoutInflater li=(LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
    for(int i=0;i<3;i++){ 
     v=li.inflate(R.layout.addit, null); //v is a View declared as private member of the class 
     t=(TextView) v.findViewById(R.id.a1);//t is TextView declared as private member 
     EditText e=(EditText) v.findViewById(R.id.e1); 
     e.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 

      } 

      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
        int arg3) { 

      } 

      public void afterTextChanged(Editable arg0) { 
       ret(v, t); 
      } 
     }); 
     ll.addView(v); 

    } 

我RET功能

public void ret(View v,TextView t){ 
    EditText e=(EditText) v.findViewById(R.id.e1); 
    t.setText(e.getText()); 

} 

主要XML:

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/i1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    </LinearLayout> 

addit.xml看起来像这样

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/l2" 
android:layout_width="match_parent" 
android:layout_height="100dp" 
android:orientation="vertical" > 
<TextView 
    android:id="@+id/a1" 
    android:layout_width="fill_parent" 
    android:layout_height="40dp" 
    android:text="" /> 
<EditText 
    android:id="@+id/e1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" > 

    <requestFocus /> 
</EditText> 

</LinearLayout> 

听者只监听在过去的变化EditText视图的...如何让它听取所有三个EditText视图? 感谢..

回答

1

这是因为你的vt变量在每个循环覆盖,使它们指向最后一个视图和TextView的。

试着改变你的代码:

final View v=li.inflate(R.layout.addit, null); 
final TextView t=(TextView) v.findViewById(R.id.a1); 
+0

由于解决了这个问题.... – karyboy 2012-01-03 11:37:52

0

请执行下列操作。

 edit_ll = (LinearLayout) findViewById(R.id.EditLinearLayout); 

    addEditText(); 

    private void addEditText() { 

     LayoutInflater li = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
     for(int i = 0; i < 3; i++) { 
      View v = li.inflate(R.layout.addit, null); 
      EditText e = (EditText) v.findViewById(R.id.e1); 
      e.addTextChangedListener(new TextWatcher() { 

       public void afterTextChanged(Editable s) { 
        // TODO Auto-generated method stub 

       } 

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

       } 

       public void onTextChanged(CharSequence s, int start, 
        int before, int count) { 
        // TODO Auto-generated method stub 

        System.out.println("--change--"); 
       } 

      }); 
      edit_ll.addView(v); 
     } 


    } 

它让所有ediText onTextChangeListener。尝试一下。

+0

感谢您的答复..我相信这会工作太... – karyboy 2012-01-03 17:28:40

+0

这对我的作品。如果这对你有帮助,请接受答案。 – Debarati 2012-01-04 05:02:18