Cómo agregar otro EditText cuando haga clic y llenar otro (Android)

Yo estaba googling por un tiempo, pero no puedo encontrar la forma de agregar un otro EditText después de hacer clic en uno.

Trato de describir mi problema por esta imagen:

Introduzca aquí la descripción de la imagen

¿Hay algún contenedor que proporcione esta funcionalidad?

Mira esto:

public class YourActivity extends Activity { private LinearLayout holder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity); //get a reference to the LinearLayout - the holder of our views holder = (LinearLayout) findViewById(R.id.holder); addNewEdit(); } private void addNewEdit() { //inflate a new EditText from the layout final EditText newEdit = (EditText) getLayoutInflater().inflate(R.layout.new_edit, holder, false); //add it to the holder holder.addView(newEdit); //set the text change lisnter newEdit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //here we decide if we have to add a new EditText view or to //remove the current if (s.length() == 0 && holder.getChildCount() > 1) { holder.removeView(newEdit); } else if (s.length() > 0 && ((before + start) == 0)) { addNewEdit(); } } @Override public void afterTextChanged(Editable s) { } }); } } 

El your_activity.xml :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/holder" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout> 

Y el new_edit.xml :

 <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> </EditText> 

EDIT: Por supuesto, usted tiene que establecer los rellenos correctos / márgenes, y probablemente crear sus propios diseños y estilo para el titular y los elementos que inflar.

Puede agregar todos los 5 a su XML y asignar a android:visibility="gone" a todos, pero primero uno. A continuación, debe asignar un TextWatcher a cada uno de ellos, pero por la simplicidad sólo se mostrará para uno.

 et1.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { et2.setVisibility(TextUtils.isEmpty(s.toString()) ? View.GONE : View.VISIBLE); // The rest of them also??? } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); 

Si no desea un número fijo de ellos, deseará crear programáticamente EditTexts y luego agregarlos a uno de los ViewGroups (RelativeLayout, LinearLayout, FrameLayout, …) llamando addView método addView

  • Interferencia de la interfaz de usuario de Android al configurar el color de ActionBar
  • Cómo obtener texto de EditText?
  • WebView Atrás, Actualizar, Reenviar? Simplemente no funciona!
  • ¿Cómo podemos diseñar interfaces de usuario en Eclipse?
  • Diseño deslizante como google maps v7
  • Android: NPE en TabSpec setContent (Ver)
  • Pasar al siguiente fragmento con un clic de botón
  • ¿Cómo diseñar una aplicación Android adecuada? Consejos y trucos
  • Recuperación y análisis de datos de iCal a través de HTTP en Android
  • Andengine: Añade una vista que contiene los controles de interfaz de usuario en la parte superior de andengine usando addContentView
  • Optimizar el diseño complejo
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.