2017-04-01 45 views
-2

我添加加的EditText在我的活動 enter image description here安卓計數所有的EditText

我不能calulate所有的EditText值

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(); 
    } 
} 

如何在EditText上和值和用戶可以添加更多的EditText價格

代碼添加

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(); 
     } 
    }); 

回答

0

使用下面的方法您的加號圖標來計算所有EditTexts的總和:

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

謝謝你<3 它的工作原理 –

0

試試這個XM L以下,

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

更換imageview的

+0

我覺得在代碼的Java問題 –

+0

爲什麼ü做動態創建UI的 –

+0

用戶界面不是我的問題 –