How to create Edittext programmatically inside of

2019-09-05 08:24发布

问题:

I have created Dialog Box with 4 EditText. But now I need to do this by dynamically depends on API response JsonObject count.

Here is my response:

{  
   "additional_charges":{  
      "1":"121324324",
      "2":"245657687",
      "3":"379809733",
      "4":"4467797894"
   }
}

If "additional_charges", has "1","2","3" ---> I have to show 3 Edittext.

If "additional_charges", has "1","2","3","4" ---> I have to show 4 Edittext in Dialog box.

How to do this? dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fare_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/currentlocTxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="center"
        android:lines="1"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="5dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="@string/additionalcharge"
        android:textColor="@color/button_accept"
        android:textSize="15sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="5dp"
        android:id="@+id/chargeType"
        android:orientation="vertical"
        android:padding="3dp"
        android:weightSum="1">


    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="@dimen/com_facebook_share_button_compound_drawable_padding"
        android:background="@color/hintcolor" />

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

        <Button
            android:id="@+id/cancelBut"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="0.5"
            android:background="@color/white"
            android:text="Cancel"
            android:textColor="@color/button_accept" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/com_facebook_share_button_compound_drawable_padding"
            android:background="@color/hintcolor" />

        <Button
            android:id="@+id/submitBut"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:background="@color/white"
            android:text="Submit"
            android:textColor="@color/button_accept" />
    </LinearLayout>
</LinearLayout>

Dialog.class:

private void ShowDialog(final String completeUrl, Context context) {
            if (NotificationAct.passenger_mobile != null) {

                final View view = View.inflate(OngoingAct.this, R.layout.additional_charges, null);
                cDialog = new Dialog(OngoingAct.this, R.style.dialogwinddow);
                cDialog.setContentView(view);
                cDialog.setCancelable(false);
                cDialog.show();
                FontHelper.applyFont(OngoingAct.this, cDialog.findViewById(R.id.alert_id));

                LinearLayout linlay_container = (LinearLayout) findViewById(R.id.chargeType);

                //length from json response
                int arrayLength = 4;
                EditText editText[] = new EditText[0];
                for (int i = 0; i < arrayLength; i++) {
                    editText[i] = new EditText(this);
                    editText[i].setBackgroundResource(R.color.white);
                    editText[i].setId(i);
                    linlay_container.addView(editText[i]);
                }

                final Button button_success = (Button) cDialog.findViewById(R.id.submitBut);
                final Button button_failure = (Button) cDialog.findViewById(R.id.cancelBut);
                button_failure.setVisibility(View.VISIBLE);

              }
}

回答1:

Firstly, parse the Json response and get the length of the array.

Then, create a layout container in the Dialog box where you want to add the EditText's.

Then, you can iterate through for loop by creating new EditText object and adding it to the container according to the array list.

Have a look on a code snippets,

 LinearLayout linlay_container=(LinearLayout)findViewById(R.id.linlay_container);
    //length from json response
    int arrayLength=?;
    for (int i = 0; i <arrayLength ; i++) {
        EditText editText=new EditText(this);
        linlay_container.addView(editText);
    }

Here, linlay_container is the container layout of Dialog box in which you want to add EditText and arraylength is the array size from your json response.