android - 如何从 android 中膨胀的多个 LinerLayouts 中获取 Editexts 的值?

标签 android android-linearlayout layout-inflater android-inflate

我做了一个简单的android Activity ,因为我有一个加号按钮和一个减号按钮,当我单击加号按钮时,带有一些EditTexts的 View (行)将被膨胀到LinearLayout中,点击添加按钮的次数,所以我成功完成了,现在我想获取每个 EditText 值,但是我搜索了很多并且卡在了这里,所以请帮助我,我的代码如下: 行.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hiddenLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <LinearLayout
        android:id="@+id/ll_a"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv_del"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:weightSum="5" >

        <TextView
            android:id="@+id/lblb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Item name/ID  :"
            android:textSize="12dp" />

        <EditText
            android:id="@+id/et_item_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/item_id_txt"
            android:inputType="number"
            android:paddingLeft="5dp"
            android:singleLine="true"
            android:textSize="12dp" />
    </LinearLayout>

    <TextView
        android:id="@+id/lblz"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll_a"
        android:text="Description :"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lblz"
        android:layout_marginTop="5dp"
        android:background="@drawable/desrciption"
        android:gravity="center_vertical"
        android:hint="Description"
        android:inputType="text"
        android:padding="2dp"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/tv_qt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_desc"
        android:layout_marginLeft="15dp"
        android:text="Quantity"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_qty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_qt"
        android:background="@drawable/amount_txt"
        android:ems="5"
        android:inputType="number"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_ut_prz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_desc"
        android:layout_marginLeft="25dp"
        android:layout_toRightOf="@+id/et_qty"
        android:text="Unit Price"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_unit_prize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_ut_prz"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/et_qty"
        android:background="@drawable/amount_txt"
        android:ems="5"
        android:inputType="number"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_amt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_desc"
        android:layout_marginLeft="50dp"
        android:layout_toRightOf="@+id/tv_ut_prz"
        android:text="Amount"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_amt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tv_amt"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/et_unit_prize"
        android:background="@drawable/amount_txt"
        android:ems="5"
        android:inputType="number"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:textSize="12dp" />

    <ImageView
        android:id="@+id/iv_del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:background="@drawable/minus" />
<View 
    android:layout_width="fill_parent"
    android:layout_height="0.75dp"
    android:background="#cecece"
    android:layout_marginTop="10dp"
        android:layout_below="@+id/et_amt"
    />
</RelativeLayout>

代码:

//code to adding layout on buttonClick

case R.id.iv_add:

    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View addView = layoutInflater.inflate(R.layout.raw_descs, null);
    ImageView buttonRemove = (ImageView) addView.findViewById(R.id.iv_del);
    et_item_id = (EditText) addView.findViewById(R.id.et_item_id);
    et_desc = (EditText) addView.findViewById(R.id.et_desc);
    et_qty = (EditText) addView.findViewById(R.id.et_qty);
    et_unit_prize = (EditText) addView.findViewById(R.id.et_unit_prize);
    et_amt = (EditText) addView.findViewById(R.id.et_amt);

    et_qty.addTextChangedListener(textwatcher);
    et_unit_prize.addTextChangedListener(textwatcher);

    buttonRemove.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            ((LinearLayout) addView.getParent()).removeView(addView);
calculateInvoice();
            if (cnt >= 0) {
                cnt = cnt - 1;
            }

        }
    });
    cnt = cnt + 1;
    listitems.setTag(cnt);
    listitems.addView(addView);
calculateInvoice();
    break;


  private void calculateInvoice() {

    double QuantyInt = 1;
    double PriceInt = 0;
    double discountInt = 0;
    double shipInt = 0;

    for (int i = 0; i < listitems.getChildCount(); i++) {

        et_qty = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(4);

        et_unit_prize = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(6);

        if (et_qty != null) {
            QuantyInt = Double.parseDouble(!et_qty.getText().toString().equals("") ? et_qty.getText().toString() : "0");

        }
        if (et_unit_prize != null) {
            PriceInt = Double.parseDouble(!et_unit_prize.getText().toString().equals("") ? et_unit_prize.getText().toString() : "0");

        }
        subtotal = subtotal + (QuantyInt * PriceInt);

    }

    double textResult = subtotal;
    System.out.println("::::::::::::MY TOATAL PRICE::::::::::::::::>>>>>>>>>>>>>>>>" + subtotal);
    et_amt.setText("$" + textResult + "");
    tv_pre.setText("$" + textResult + "");

    if (et_dis != null) {
        discountInt = Double.parseDouble(!et_dis.getText().toString().equals("") ? et_dis.getText().toString() : "0");

    }
    discount = ((subtotal * discountInt) / 100);
    double txtdiscount = discount;
    tv_dis.setText("$" + txtdiscount + "");
    double totl1 = subtotal - discount;
    tv_total.setText("$" + totl1 + "");

    if (et_ship != null) {
        shipInt = Double.parseDouble(!et_ship.getText().toString().equals("") ? et_ship.getText().toString() : "0");

    }
    tv_ship.setText("$" + et_ship.getText().toString());
    double finaltotal = (shipInt + totl1);
    tv_total.setText("$" + finaltotal + "");

}

TextWatcher textwatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
        calculateInvoice();

    }
};

这样我正在 inflated layout ,请帮助我如何从每个膨胀的布局中获取 edittexts 值?

最佳答案

点击按钮时,添加addView到全局ArrayList<ViewGroup> listOfViewGroups .

编辑:根据您在评论中提供给我的信息,这是您必须要做的(我不能说得比这更清楚)。编写以下方法:

public double getTotal() {

    double total = 0;

    //get each ViewGroup
    for(int i = 0; i < listOfViewGroups.size(); i++) {
         ViewGroup vg = listOfViewGroups.get(i);
         //get each EditText containing the amount
         EditText e = (EditText) v.findViewById(R.id.et_amt);
         //get the string entered in the EditText
         String str = e.getText().toString();
         //change to a double
         double amount = Double.parseDouble(str);
         //Add to total
         total = total + amount;
    }

    return total;
}

当您准备好添加所有值时,您必须调用该方法。我不能告诉你什么时候你必须调用它,因为只有你知道,我不知道你什么时候想调用。

调用该方法后,将返回的 double 设置为 TextView 的文本这将显示总数。例如,

tv_total.setText(""+getTotal());

(我假设您的 TextView 显示为 tv_total

关于android - 如何从 android 中膨胀的多个 LinerLayouts 中获取 Editexts 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24838162/

相关文章:

android - 火库 : PERMISSION_DENIED: Missing or insufficient permissions

android - 蓝牙接收器响应时间

android - 出现虚拟键盘时适配组件

java - 单击按钮后关闭警报对话框

android - 最好在 Android 中膨胀或实例化控件?

java - android java 膨胀异常

android - 单击电话号码文本时如何显示电话窗口

android - 工作完成后无法停止工作人员(ListenableWorker)任务

android - Android布局真的很难成倍增长吗?

android - TextView 因 android 中父级的 LinearLayout wrap_content 而断线