开发者

Dynamically add edit text to a layout

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"
      开发者_如何学Go  />

      <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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜