Динамически добавлять редактируемый текст в макет - PullRequest
0 голосов
/ 27 июня 2011

Я пытаюсь динамически добавить представление для редактирования текста в макет из класса Java.Однако из того, что я реализовал, ничего не меняется в моих действиях.Вот что у меня есть:

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>

Всякий раз, когда я нажимаю на мою кнопку, действие слышно, но ничего не делается.Макет страницы не меняется, даже если новый текст редактирования находится в моем списке редактируемых текстов, и я добавляю новый текст редактирования в макет.

Любая помощь будет принята с благодарностью.

1 Ответ

1 голос
/ 27 июня 2011

Он добавляется, но вы его не видите, потому что не осталось места.Две вещи для проверки:

  • Поместите LinearLayout внутрь ScrollView.
  • Используйте параметр веса.

Используя параметр веса:

EditText temp = new EditText(ContactEdit.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...