0

두 번째 부풀린 뷰에서 사용자 입력을받을 수 없다는 문제가 있습니다. 아직 이미지를 직접 게시 할 수 없으므로이 사이트의 사진을 http://postimage.org/image/b4syhdzrr/ 으로 업로드했습니다.동적으로 생성 된 뷰에서 사용자 입력 받기

당신이 볼 수 있듯이, 텍스트 뷰는 상단이 글고 입력 + 계산을 표시, 세 번째 입력은 그러나 고려되지 않았다. (번호의 첫 번째 글고 치기가 팽창하지 않음)

아니에요으로 최종 사용자가 얼마나 많은 EditText를 필요로하는지 확인하십시오. 모든 EditText에서 사용자 입력을 얻으려면 어떻게해야합니까?

여기 내가 TextPatrics를 OnClickListener 내부의 TextWatcher 안에 저장하도록 설정했지만이 코드를 사용하면 TextWatcher가 비어 있어도 Plus 단추의 OnClickListener가 충돌합니다.

public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      int position = spinner.getSelectedItemPosition(); 
      switch (position) { 
      case 0: 
       ll = (LinearLayout) findViewById(R.id.llSetView); 
       ll.removeAllViews(); 
       View person1 = View.inflate(BillCalculator1.this,R.layout.person1, null); 
       ll.addView(person1); 

       btP1Add = (Button) ll.findViewById(R.id.buttonP1Add); 
       btP1Gst = (Button) ll.findViewById(R.id.buttonP1GST); 
       btP1Add.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         llNewRow = (LinearLayout) findViewById(R.id.llP1AddNewRow); 
         etNewRow = (EditText) findViewById(R.id.editTextNewRow); 
         View newRow = View.inflate(BillCalculator1.this,R.layout.newrow, null); 
         llNewRow.addView(newRow); 

         TextWatcher input = new TextWatcher() { 
          public void afterTextChanged(Editable s) { 
          } 
          //SharedPreferences here 

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

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

        } 
       }); 

프로그래밍 및 안드로이드에 부풀려

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<EditText 
    android:id="@+id/editTextNewRow" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="numberDecimal" 
    android:hint="Enter new amount"> 

    <requestFocus /> 
</EditText> 

아주 새로운을위한 XML, 대단히 감사합니다, 추가 정보 필요가 있으면 알려 않습니다.

+0

합계를 자동으로 계산 하시겠습니까? 아니면 단추 중 하나를 클릭했을 때 하단에 있습니다 (예 :'Add gst','add vsc')? – Luksprog

+0

충돌이 발생할 때 스택 추적은 무엇입니까? 추측 해 보면 R.id.editTextNewRow는 행 레이아웃에있는 ID를위한 것이지만 최상위 레이아웃의 findViewById에 전달하는 것입니다. 아마도 newRow.findViewById에 전달하고 싶을 것입니다. 당신이 게시 한 코드에서 etNewRow로 아무 것도하지 않기 때문에 이것이 어떻게 문제를 일으키는 지 확신 할 수 없습니다. – Iain

+0

@Luksprog, 버튼 클릭시 완벽 할 것입니다. –

답변

0

나는 당신이하려고하는 것에 대한 또 다른 접근법을 시도 할 것이다. 나는 특별 TextWatcher을 부풀려진 EditText에 추가하고 사용자가 입력 한 값을 ArrayList에 저장합니다. 우선 당신은 당신의 클래스에서 두 개의 추가 필드가 필요합니다 : 그것은 때

int position = spinner.getSelectedItemPosition(); 
switch (position) { 
case 0: 
    mData.clear(); 
    counter = 0; 
    ll = (LinearLayout) findViewById(R.id.llSetView); 
    ll.removeAllViews(); 
    // I sure hope that you have the Buttons with the id button1Add and buttonP1GST in the layout that you inflate 
    // otherwise the code will throw a NullPointerException when you use them below 
    View person1 = View.inflate(BillCalculator1.this,R.layout.person1, null); 
    ll.addView(person1); 
    btP1Add = (Button) ll.findViewById(R.id.buttonP1Add); 
    btP1Gst = (Button) ll.findViewById(R.id.buttonP1GST); 
    btP1Add.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // add an entry in the data holder for this row 
     mData.add(""); 
     View newRow = View.inflate(BillCalculator1.this, 
         R.layout.newrow, null); 
     EditText justAdded = (EditText) newRow 
         .findViewById(R.id.editTextNewRow); 
     justAdded.addTextChangedListener(new InputWatcher(counter)); 
     ll.addView(newRow); 
     counter++; 
      } 
    }); 

: 다음

public class InputWatcher implements TextWatcher { 

      private int mRowNumber; 

      public InputWatcher(int rowNumber) { 
       mRowNumber = rowNumber; 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // here you'll add the text as the user enters it in the correct position in the 
       mData.set(mRowNumber, s.toString());      
      } 
    } 

:

private ArrayList<String> mData = new ArrayList<String>(); // this will store the entered values 
private static int counter = 0; // to identify the rows 

이 같은 TextWatcher 인터페이스를 구현하는 클래스를 확인 시간을 합계를 계산, ButtonOnClickListener, 그냥 :

@Override 
public void onClick(View v) { 
     int storedSize = mData.size(); 
    int sum = 0; 
    for (int i = 0; i < storedSize; i++) { 
     sum += Integer.parseInt(mData.get(i).equals("") ? "0" 
          : mData.get(i)); 
    } 
    Toast.makeText(getApplicationContext(), "Total" + sum,    Toast.LENGTH_SHORT).show(); 
} 

귀하의 사진에서 이미 EditText (+ Button의 왼쪽에있는 하나)으로 시작하면 이해가되지 않습니다. 이 경우 행을 추가 할 InputWatcher을 추가하기 전에 수동으로 InputWatcher을 설정하고 counter 증분을 이동해야합니다.

+0

도움을 주셔서 감사합니다. –

+0

@DavidLamborghiniTan'llSetView'''LinearLayout'을 포함하는 레이아웃 파일과''R.layout.person1'을 팽창시킨 레이아웃 파일을 포스트하면 작동하지 않습니다. – Luksprog

+0

완벽하게 작동합니다. 도움에 감사드립니다. 크게 감사드립니다. –

관련 문제