2016-08-29 3 views

Antwort

1

activity_main.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="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/ll_edit_texts_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/et1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/buttonAdd" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ADD" /> 
</LinearLayout> 

und Activity

final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container); 
     Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd); 
     buttonAdd.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       EditText editText = new EditText(getActivity()); 
       //editText.setId(); you should set id smartly if you wanted to use data from this edittext 
       llEditTextsContainer.addView(editText); 
      } 
     }); 
+0

@Ajay kumar hast du meine Antwort Hilfe? – Nikhil

+1

Ja Nikhil !!! funktioniert gut ... Danke !!! –

0

allererst in Ihrem folgenden Code setzt einen Container Ansicht in dem Haupt Layout zu erstellen, die die neuen Edittexts halten wird.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.vuclip.dynamictextedit.MainActivity"> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/addTextView" 
    android:text="Add EditText" 
    android:onClick="onClick" 
    /> 

<TableLayout 
    android:id="@+id/containerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</TableLayout> 

Hier, auf die Taste drücken, werden neue Edittexts erstellt und in das Tablelayout hinzugefügt werden.

Jetzt erstellen Sie ein neues Layout, das Ihren editiertext enthält (ich nannte diese Datei new_layout.xml).

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

<EditText 
    android:id="@+id/newEditText" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

Jetzt dieses Layout in Ihre Haupttätigkeit hinzuzufügen.

public class MainActivity AppCompatActivity erstreckt {

TableLayout container; 
static int rowIndex = 0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    container = (TableLayout) findViewById(R.id.containerLayout); 
} 

public void onClick(View view) { 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View newRowView = inflater.inflate(R.layout.new_layout,null); 
    container.addView(newRowView,rowIndex); 
    rowIndex++; 
}} 
Verwandte Themen