Android Dynamic Add Delete multiple controls

Source: Internet
Author: User

Novice on the road, did not find dynamic add to delete multiple controls, tinkering with a, do wrong place trouble everybody say

Activity_main.xml:

<button
Android:id= "@+id/add"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:onclick= "Myclick"
android:text= "Add"/>
<!--dynamic Add-on
<linearlayout
Android:id= "@+id/addlayout"
Android:layout_width= "Match_parent"
Android:layout_above= "@+id/surebutton"
android:layout_height= "Match_parent"
android:layout_below= "@+id/add"
android:orientation= "Vertical" >
</LinearLayout>

<button
Android:id= "@+id/surebutton"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
Android:layout_alignparentright= "true"
Android:layout_centervertical= "true"
android:onclick= "Myclick"
android:text= "OK"/>

<!--Adding an object is a layout file--

Addlayout.xml:

<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" >

<button
Android:id= "@+id/delete"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_margintop= "10DP"
Android:layout_alignparentright= "true"
android:background= "@drawable/delete"
android:onclick= "Myclick"/>

<button
Android:id= "@+id/unit"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignbaseline= "@+id/delete"
android:layout_toleftof= "@+id/delete"
android:onclick= "Myclick"
android:text= "None"/>

<edittext
Android:id= "@+id/edittext"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignbaseline= "@+id/delete"
android:layout_toleftof= "@+id/unit"/>

</RelativeLayout>

Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Map.Entry;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.LinearLayout;

public class Mainactivity extends Activity implements Onclicklistener {
LinearLayout ll;

I use the list to add more than one object after the control can not be deleted, if someone knows how to do trouble tell me, novice small white a ...
Map<integer, button> DeleteButton;
Map<integer, button> Unitbutton;
Map<integer, edittext> EditText;
Map<integer, view> VI;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
ll = (linearlayout) Findviewbyid (r.id.addlayout);
DeleteButton = new Hashmap<integer, button> ();
Unitbutton = new Hashmap<integer, button> ();
EditText = new Hashmap<integer, edittext> ();
VI = new Hashmap<integer, view> ();

}
Get Add Object
Public View Getpartview () {
Return Layoutinflater.from (This). Inflate (r.layout.addlayout, NULL);
}

int tag = 0;//loop

public void Myclick (View v) {
Switch (V.getid ()) {
Case R.id.add:
View view = Getpartview ();
Add Right Delete button
Button button = (button) View.findviewbyid (R.id.delete);
Button.setbackgroundresource (R.drawable.delete);

The reason why the tag does not use ID is that the ID and other controls are duplicated when listening, and the other control can respond to the ID of the setting.
Button.settag (tag);
Button.setonclicklistener (this);
Deletebutton.put (tag, button);
The middle button, want to replace spinner, now do not do, can ignore
Button Changebutton = (button) View.findviewbyid (r.id.unit);
Changebutton.setonclicklistener (this);
Unitbutton.put (tag, Changebutton);
Add Input Box
EditText edit = (EditText) View.findviewbyid (R.id.edittext);
Edittext.put (tag, edit);
Ll.addview (view);//Add a View object to the layout file
Vi.put (tag, view);
System.out.println ("Addtag:" + tag);
tag++;
Break
Case R.id.surebutton:
Iterator iter = Edittext.entryset (). Iterator ();
while (Iter.hasnext ()) {
Entry Entry = (Entry) iter.next ();
System.out.println (Entry.getkey () + ":"
+ ((EditText) Entry.getvalue ()). GetText (). toString ());
}
Default
Break
}
}

@Override
public void OnClick (View v) {

Loop traversal mode of HashMap
Iterator iter = Deletebutton.entryset (). Iterator ();
while (Iter.hasnext ()) {
Entry Entry = (Entry) iter.next ();
if (V.gettag (). Equals (Entry.getkey ())) {
Ll.removeview (Vi.get (V.gettag ()));
Deletebutton.remove (V.gettag ());
Unitbutton.remove (V.gettag ());
Edittext.remove (V.gettag ());
Break
}
}

}
}

Android Dynamic Add Delete multiple controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.