2013-02-26 6 views
13

LinksWie man Button in Android dynamisch erstellt?

Ich möchte eine Seite wie diese erstellen. Diese 7 Schaltflächen sind bereits vorhanden, aber wenn der Benutzer weitere Kategorien hinzufügen möchte (Schaltfläche), kann er die Schaltfläche + verwenden und mit der Schaltfläche - löschen. Irgendeine Idee oder Tutorial für das machen?

Antwort

22

Erstellen/Entfernen onClick von + button und - button wie folgt:

public void onClick(View v) { 

    switch(v.getId()){ 
    case (R.id.plusbutton): 
       Button myButton = new Button(this); 
       myButton.setText("Add Me"); 

       LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout); 
       LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
       ll.addView(myButton, lp); 
       break;. 
    case (R.id.minusbutton): 
       Button myButton = new Button(this); 
       myButton.setText("Remove Me"); 

       LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout); 
       LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
       ll.removeView(myButton, lp); 
       break; 
      } 
     } 
+0

Vielen Dank sir :) schätze deine Hilfe –

+3

Was ist Buttonlayout? wie man das schafft –

8

dies ist für Schaltfläche erstellen dynamisch in android

LinearLayout row2 = (LinearLayout) findViewById(R.id.hll2); 
Button ivBowl = new Button(this); 
ivBowl.setText("hi"); 
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70); 
layoutParams.setMargins(5, 3, 0, 0); // left, top, right, bottom 
ivBowl.setLayoutParams(layoutParams); 
row2.addView(ivBowl); 
+1

+1 gute Antwort. – Aravin

+0

@Aravinth thanks bro – Rohit

3

Es ist ziemlich einfach.

Button button1=new Button(context); 
    button1.setText("test"); 
    button1.setId(id); 
containerlayout.add(button1); 

Hoffe das hilft Ihnen.

4
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.yourlayoutidthatisonethepicture); 

Button addButton =new Button(this); 
addButton.setText("add"); 

mainLayout.addView(addButton); 

entfernen das gleiche ist gerade dieses "mainLayout.addView(addButton)" zu RemoveView oder setVisibility der Taste ändern, um View.GONE

+0

Immer noch nicht "R.id.yourlayoutidthisondethepicture" Bitte sagen Sie mir –

+0

Erledigt: D danke Mann! yo sind Genie: D –

+0

froh, ich könnte helfen;) –

0

Wenn Sie eine dynamische Ansicht erstellen möchten (wie EditText, Textview usw.), dann benutzen Sie einfach diesen Code und führen ihn in Ihrer Anwendung aus.

MyActivity.java://your Java-Datei

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1); 
EditText et = new EditText(v.getContext()); 
et.setText("My new Edit Text); 
et.setMinLines(1); 
et.setMaxLines(3); 
ll.addView(et); 

In XML-Datei:

<LinearLayout 
android:id="@+id/linearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_alignBottom="@+id/TextView01" 
android:layout_below="@+id/relativeLayout1" 
android:orientation="vertical" >