0

私のレイアウトでは、追加ボタンをクリックして2つの編集テキストを追加し、その追加(機能)し、削除ボタンをクリックすると動的編集テキストも削除されます.私の質問は、動的に2つ追加すると仮定して、そこから文字列値を取得する方法です.不要な場合は削除します。ここに私のコードがあります..

public class PartDetails extends AppCompatActivity {

    // Parent view for all rows and the add button.
    private LinearLayout mContainerView;
    // The "Add new" imageButton
    private ImageButton mAddImageButton;
    Button submit_part_details;
    private View mExclusiveEmptyView;
    EditText prequired,pnumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_part_details);

        mContainerView = (LinearLayout) findViewById(R.id.parentView);
        submit_part_details = (Button) findViewById(R.id.submit_part_details);
        mAddImageButton = (ImageButton) findViewById(R.id.add_et_parts);
        prequired= (EditText) findViewById(R.id.et_Prequired);
        pnumber= (EditText) findViewById(R.id.et_Pnumber);
        mAddImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inflateEditRow();
            }
        });
        submit_part_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String parts=prequired.getText().toString();
                String pnum =pnumber.getText().toString();
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // TODO: Handle screen rotation:
        // encapsulate information in a parcelable object, and save it
        // into the state bundle.

    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // TODO: Handle screen rotation:
        // restore the saved items and inflate each one with inflateEditRow;

    }

    // Helper for inflating a row
    private void inflateEditRow() {

        LayoutInflater layoutInflater =
                (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
        final EditText parts_required = (EditText) addView
                .findViewById(R.id.et_parts);
        final EditText parts_number = (EditText) addView
                .findViewById(R.id.et_Pnumber);
        final ImageButton deleteButton = (ImageButton) addView
                .findViewById(R.id.delete_et_parts);
        deleteButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ((LinearLayout) addView.getParent()).removeView(addView);
            }
        });

        mContainerView.addView(addView);
    }
}

これは親 XML レイアウトです

  <?xml version="1.0" encoding="utf-8"?>

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:background="@android:color/black"
            android:layout_height="match_parent"
            android:focusableInTouchMode="true">


            <LinearLayout
                android:id="@+id/parentView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:layout_below="@+id/tv"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:orientation="vertical">

                <RelativeLayout
                    android:id="@+id/relative_layout1"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/parts_required"
                        android:layout_width="110dp"
                        android:layout_height="40dp"
                        android:text="Parts Required"
                        android:textColor="#fff"
                        android:textSize="15sp"
                        android:textStyle="bold"
                        android:background="@drawable/shape"
                        android:gravity="center"/>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_toRightOf="@+id/parts_required"
                        android:layout_marginLeft="20dp"
                        android:orientation="horizontal">

                        <EditText
                            android:id="@+id/et_Prequired"
                            android:layout_width="0dp"
                            android:layout_height="40dp"
                            android:layout_weight="0.8"
                            android:hint="text"
                            android:singleLine="true"
                            android:inputType="text"
                            android:textSize="20sp"
                            android:paddingLeft="20dp"
                            android:imeOptions="actionNext"
                            android:background="@drawable/shape_edit_text"
                            />

                        <ImageButton
                            android:id="@+id/add_et_parts"
                            android:layout_width="0dp"
                            android:layout_height="40dp"
                            android:background="@null"
                            android:onClick="onAddNewClicked"
                            android:src="@android:drawable/ic_input_add"
                            android:layout_weight="0.2"/>


                    </LinearLayout>


                </RelativeLayout>
<RelativeLayout
                android:id="@+id/relative_layout2"
                android:layout_marginTop="5dp"
                android:layout_below="@+id/relative_layout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/parts_number"
                    android:layout_width="110dp"
                    android:layout_height="40dp"
                    android:text="Parts Number"
                    android:textColor="#fff"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    android:background="@drawable/shape"
                    android:gravity="center"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/parts_number"
                    android:layout_marginLeft="20dp"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/et_Pnumber"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_weight="1"
                        android:hint="text"
                        android:singleLine="true"
                        android:inputType="text"
                        android:textSize="20sp"
                        android:paddingLeft="20dp"
                        android:imeOptions="actionNext"
                        android:background="@drawable/shape_edit_text"
                        />

                    <!-- <ImageButton
                         android:id="@+id/add_et_parts_number"
                         android:layout_width="0dp"
                         android:layout_height="50dp"
                         android:background="@null"
                         android:src="@android:drawable/ic_input_add"
                         android:layout_weight="0.2"/>-->

                </LinearLayout>

            </RelativeLayout>


                <Button
                    android:layout_marginTop="50dp"
                    android:id="@+id/submit_part_details"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_below="@+id/snap"
                    android:textAllCaps="false"
                    android:textSize="20sp"
                    android:textColor="#fff"
                    android:background="#1c4648"
                    android:text="Submit All Details" />

            </RelativeLayout>



        </RelativeLayout>
    </ScrollView> 

これは Add Button XML レイアウトです

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@android:color/black"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/relative_layout1"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/parts_required"
            android:layout_width="110dp"
            android:layout_height="40dp"
            android:text="Parts Required"
            android:textColor="#fff"
            android:textSize="15sp"
            android:textStyle="bold"
            android:background="@drawable/shape"
            android:gravity="center"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/parts_required"
            android:layout_marginLeft="20dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/et_parts"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="0.8"
                android:singleLine="true"
                android:inputType="text"
                android:textSize="20sp"
                android:hint="text"
                android:paddingLeft="20dp"
                android:imeOptions="actionNext"
                android:background="@drawable/shape_edit_text"
                />

            <ImageButton
                android:id="@+id/delete_et_parts"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:background="@null"
                android:src="@android:drawable/ic_delete"
                android:layout_weight="0.2"/>


        </LinearLayout>


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relative_layout2"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/relative_layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/parts_number"
            android:layout_width="110dp"
            android:layout_height="40dp"
            android:text="Parts Number"
            android:textColor="#fff"
            android:textSize="15sp"
            android:textStyle="bold"
            android:background="@drawable/shape"
            android:gravity="center"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/parts_number"
            android:layout_marginLeft="20dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/et_Pnumber"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:hint="text"
                android:singleLine="true"
                android:inputType="text"
                android:textSize="20sp"
                android:paddingLeft="20dp"
                android:imeOptions="actionNext"
                android:background="@drawable/shape_edit_text"
                />

            <!-- <ImageButton
                 android:id="@+id/add_et_parts_number"
                 android:layout_width="0dp"
                 android:layout_height="50dp"
                 android:background="@null"
                 android:src="@android:drawable/ic_input_add"
                 android:layout_weight="0.2"/>-->

        </LinearLayout>

    </RelativeLayout>


</LinearLayout> 
4

2 に答える 2

0
  ArrayList<EditText> edtParts_required=new ArrayList<EditText>();
  ArrayList<EditText> edtParts_number=new ArrayList<EditText>();

// Helper for inflating a row
private void inflateEditRow() {
    LayoutInflater layoutInflater =
            (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
    final EditText parts_required = (EditText) addView
            .findViewById(R.id.et_parts);
    final EditText parts_number = (EditText) addView
            .findViewById(R.id.et_Pnumber);

    //Add EditText object to Collection 
     edtParts_required.add(parts_required);
     edtParts_number.add(parts_required);

    final ImageButton deleteButton = (ImageButton) addView
            .findViewById(R.id.delete_et_parts);
    deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ((LinearLayout) addView.getParent()).removeView(addView);
        }
    });

    mContainerView.addView(addView);
}

When you delete the inflated row remove the element from collection too 

Loop through your Collection and get the text from dynamic EditText
for(int i=0;i<edtParts_required.size();i++){
   String enterdText=edtParts_required.get(i).getText().toString();
}
于 2016-04-04T08:43:16.727 に答える