动态编辑文本添加到布局 [英] Dynamically add edit text to a layout

查看:99
本文介绍了动态编辑文本添加到布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态地添加编辑文本视图从Java类中的布局。但是从我已经实现了我的动作没有什么变化。这是我有:

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");
                }
            }
        });
}

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

任何帮助将是AP preciated感谢。

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:


  • 将在的LinearLayout A 滚动型
  • 里面
  • 使用权重参数。

使用加权参数:

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

这篇关于动态编辑文本添加到布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆