2012-01-03 3 views
0

EditText 객체를 n 개 만들고 싶습니다. LayoutInflater를 사용하여 n까지 실행되는 루프에서이를 수행합니다. 뷰는 레이아웃에 추가되지만 각 EditText 객체에 addTextChangeListener를 추가하려는 경우 리스너는 마지막 객체에만 추가됩니다. 종료 문제는 어떻게 해결할 수 있습니까?루프에 생성 된 각 EditText 객체에 addTextChangeListener를 추가하십시오.

<?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> 

과 같습니다

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이다 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

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

답장을 보내 주셔서 감사합니다. ..i too too ... – karyboy

+0

이 작품은 나를 위해 작동합니다. 이 방법이 도움이된다면 대답을 수락하십시오. – Debarati

관련 문제