2017-04-01 3 views
-2

ich hinzufügen und EditText in meiner Tätigkeit enter image description hereandroid Zählung alle EditText

Ich kann alle Wert EditText nicht calulate

public void listAllAddView(){ 
    int childCount = container.getChildCount(); 
    EditText[] editTexts; 
    int total = 0; 
    for(int i=0; i<childCount; i++){ 
     View thisChild = container.getChildAt(i); 
     EditText childTextView = (EditText) thisChild.findViewById(R.id.textout); 
     String childTextViewValue = childTextView.getText().toString(); 
     Toast.makeText(MainActivity.this, childTextViewValue, Toast.LENGTH_LONG).show(); 
    } 
} 

wie Wert in EditText summieren können und Benutzer kann mehr EditText Preis hinzufügen

Code

buttonAdd.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      LayoutInflater layoutInflater = 
        (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      final View addView = layoutInflater.inflate(R.layout.row, null); 
      EditText textOut = (EditText)addView.findViewById(R.id.textout); 
      textOut.setText(textIn.getText().toString()); 
      ImageView buttonRemove = (ImageView)addView.findViewById(R.id.remove); 

      final View.OnClickListener thisListener = new View.OnClickListener(){ 
       @Override 
       public void onClick(View v) { 
        ((LinearLayout)addView.getParent()).removeView(addView); 
        listAllAddView(); 
       } 
      }; 

      buttonRemove.setOnClickListener(thisListener); 
      container.addView(addView); 
      listAllAddView(); 
     } 
    }); 

Antwort

0

Verwendung Im Folgenden Methoden Summe aller EditTexts zu berechnen:

public int calculateSum() { 
    int sum=0; 
      try { 
       if (container!= null) { 

        for (int i = 0; i < container.getChildCount(); i++) { 
         EditText editText1 = getEditTextFromPosition(i); 
         String str = editText1.getText().toString(); 
         if (str != null && !str.equals("") && str.length() > 0) 
          sum= sum+ Integer.parseInt(editText1.getText().toString()); 
        } 
       } 
       txtSum.setText("" + String.valueOf(sum)); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
    return sum; 
     } 

     private EditText getEditTextFromPosition(int position) { 
      EditText editText = null; 
      LinearLayout linearLayout = (LinearLayout) container.getChildAt(position); 
      View view = linearLayout.getChildAt(0); 

      if (view instanceof EditText) { 
       editText = (EditText) view; 
      } else { 
       view = relativeLayout.getChildAt(1); 
       editText = (EditText) view; 
      } 
      return editText; 
     } 
+0

Vielen Dank <3 es funktioniert –

0

versuchen, diese xm hinzufügen l unter

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".8"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".2"> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:drawable/btn_minus" /> 
    </LinearLayout> 


</LinearLayout> 

ersetzen Sie die Plus-Symbol auf Imageview

+0

ich denke Problem in Code java –

+0

Warum sind u dynamische Erstellung von UI zu tun –

+0

UI nicht mein Problem –