Dynamically add edit text to a layout

心不动则不痛 提交于 2019-12-12 02:23:22

问题


I am trying to add an edit text view dynamically to a layout from the java class. However from what I have implemented nothing changes on my action. This is what I have:

public final void onCreate(final Bundle i){
    int value = 0;

    isEdit = false;

    try
    {
        super.onCreate(i);

        this.setContentView(R.layout.contactedit);
        ContactList.soundIndicator = 0;
        etPhoneNumber = new ArrayList<EditText>();
        this.etPhoneNumber.add((EditText) this.findViewById(R.id.etPhoneNumberInput));

        this.addNumberBtn = (Button) this.findViewById(R.id.addNumberEditText);
        addNumberBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //etPhoneNumber.add(new EditText(ContactEdit.this));
                try{
                    LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);

                    EditText temp = new EditText(ContactEdit.this);
                    temp.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));                

                    etPhoneNumber.add(temp);

                    layout.addView(temp);
                }catch(Exception e){
                    Log.d(TAG, "Failed to create new edit text");
                }
            }
        });
}

This is the xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" 
  android:baselineAligned="true" 
  android:orientation="vertical"
  android:id="@+id/contacteditll"
  android:background="@drawable/darkimg">

       <TextView
        android:id="@+id/titlePrint"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/titlePrompt"
        />

      <Spinner 
        android:id="@+id/spContactTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/titlePrompt"
        />

    <TextView 
     android:text="@string/namePrint"
     android:id="@+id/namePrint" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content">
     </TextView>

    <EditText android:layout_width="match_parent" 
    android:hint="@string/returnName" 
    android:id="@+id/etFirstNameInput" 
    android:layout_height="wrap_content"
    android:layout_toRightOf= "@+id/namePrint">
    </EditText>

     <TextView 
     android:text="@string/secondPrint"
     android:id="@+id/secondPrint" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content">
     </TextView>

    <EditText android:layout_width="match_parent" 
    android:hint="@string/returnName" 
    android:id="@+id/etSecondNameInput" 
    android:layout_height="wrap_content"
    android:layout_toRightOf= "@+id/namePrint">
    </EditText>

    <TextView android:id="@+id/numberPrint"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="@string/numberPrint">
    </TextView>

    <EditText android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:id="@+id/etPhoneNumberInput" 
    android:hint="@string/returnNumber">
    </EditText>

    <Button android:id="@+id/addNumberEditText"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
        android:freezesText="true"
    android:editable="false"
    android:text="ADD"
    />


     <include 
     android:id="@+id/customedittext"
     layout="@layout/myedittext"/>

    <TextView android:id="@+id/emailPrint" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="@string/emailPrint">
    </TextView>

    <EditText android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:id="@+id/etEmailAddressInput" 
    android:hint="@string/returnEmail">
    </EditText>

    <Button 
    android:freezesText="true"
    android:editable="false"
    android:layout_gravity="center" 
    android:layout_height="wrap_content" 
    android:id="@+id/btnSaveButton" 
    android:layout_width="wrap_content"  
    android:text="@string/save"
    android:layout_below="@+id/emailInput" 
   />

</LinearLayout>

Whenever I click on my button the action is heard but nothing is done. The layout of the page does not change even though the new edit text is in my list of edit texts and I am adding the new edit text to the layout.

Any help would be appreciated thanks.


回答1:


It sure gets added but you are not seeing it cause there is no space left. Two things to test:

  • Place the LinearLayout inside a ScrollView.
  • Use the weight parameter.

Using the weight parameter:

EditText temp = new EditText(ContactEdit.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);


来源:https://stackoverflow.com/questions/6493817/dynamically-add-edit-text-to-a-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!