Ich habe ein Layout mit einem einzigen editText, unter dem editText Ich habe eine Schaltfläche Hinzufügen hinzugefügt. Jetzt ist meine Frage, wenn ich den addieren Knopf anklicke, den ich einen anderen editText unter dem editText erhalten muss und das selbe wiederholen muss. Bitte jemand helfen, danke.Hinzufügen von EditTexts zu einem Layout dynamisch mit einer Schaltfläche Hinzufügen in android
0
A
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
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
- 1. Hinzufügen einer Schaltfläche zu einem Dialogfeld dynamisch
- 2. Hinzufügen von Layout dynamisch zu ScrollView android
- 3. Android: Hinzufügen von Ansicht dynamisch in einem verschachtelten Layout
- 4. Schaltfläche Hinzufügen zu PyQtGraph Layout
- 5. Hinzufügen einer Schaltfläche zu einem Layout und Programmabstürze. Warum?
- 6. Dynamisch hinzufügen RecyclerView zu einem LinearLayout android
- 7. Android: programmgesteuert Hinzufügen von Schaltflächen zu einem Layout
- 8. Android ID zu einem Layout hinzufügen
- 9. Android - Layout von einem anderen Modul hinzufügen
- 10. Elemente dynamisch zu einem listView Android hinzufügen
- 11. Klick-Ereignis Hinzufügen einer Schaltfläche dynamisch jQuery
- 12. hinzufügen Layout-programmatisch in einer anderen einem
- 13. Dynamisch Regionen zu Marionette Layout hinzufügen
- 14. Dynamisch TextBox in Android hinzufügen
- 15. Hinzufügen einer Schaltfläche zu MediaController
- 16. JS Hinzufügen einer Schaltfläche zu einem Listenfeld
- 17. Android- Hinzufügen von Ansicht zu Layout Layout Layout vor ActionBar
- 18. ionic - Hinzufügen einer Schaltfläche in einem Eingabefeld
- 19. Hinzufügen einer Schaltfläche zu einem WPF DataGrid
- 20. Android wie videfragment dynamisch zu meinem Layout hinzufügen
- 21. Schaltfläche zu einem Bild hinzufügen
- 22. Hinzufügen einer Schaltfläche zu einem Dialogfeld
- 23. Hinzufügen Ereignis-Listener zu dynamisch erstellten Schaltfläche
- 24. Hinzufügen von Layout zu einem anderen Layout in Qt [C++]
- 25. Schaltfläche in einem TextureView hinzufügen (Android/Java)
- 26. Hinzufügen einer Gruppe von EditText in Android auf Schaltfläche klicken
- 27. Dynamisch Hinzufügen von Ansicht oder Aufblasen Layout zu RecyclerView-Artikel
- 28. Android Hinzufügen von Schaltfläche in FrameLayout programmgesteuert
- 29. D3 Collapsible Force Layout Hinzufügen von Knoten zum Layout dynamisch
- 30. Android: Hinzufügen von Höhe zu einem Widget
@Ajay kumar hast du meine Antwort Hilfe? – Nikhil
Ja Nikhil !!! funktioniert gut ... Danke !!! –