2017-04-01 23 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

用户界面不是我的问题 –