[Reference to original address]
http://blog.csdn.net/l1028386804/article/details/47101387
:
Implementation method:
One: Customizing a component that contains EditText and buttons
Create an XML layout file for a Buttonext_layout custom component first
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal"> <EditTextAndroid:id= "@+id/edittext"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:inputtype= "text"/> <ButtonAndroid:id= "@+id/clearbutton"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:text= "Clear"/></LinearLayout>
Two: Create a custom control class Butext
public class Butext extends LinearLayout {private EditText medittext; Private Button Mbutton; Public Butext (Context context) {super (context); Populate the view with layout resources Layoutinflater Minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); Load Layout file Minflater.inflate (R.layout.buttonext_layout, this, true); Medittext = (EditText) Findviewbyid (R.id.edittext); Mbutton = (Button) Findviewbyid (R.id.clearbutton); Hookupbutton (context); }/** * Button handler */private void Hookupbutton (final context context) {Mbutton.setonclicklistener (n EW View.onclicklistener () {@Override public void OnClick (View v) {Medittext.settext (""); Toast.maketext (Context, "text box data cleared", Toast.length_short). Show (); } }); /**
* Assign value to edittext
*/
public void SetText (String txt) {
Medittext.settext (TXT);
}
}
Three: Realization of mainactivity class
Public classMainactivityextendsappcompatactivity {PrivateLinearLayout mlinearlayout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //dynamic Additions to our custom componentsMlinearlayout =(LinearLayout) Findviewbyid (r.id.main_layout); for(inti = 0; I < 3; i++) {Imageviewext EditText=NewImageviewext ( This);
Edittext.settext (i+ "); Mlinearlayout.addview (EditText); } }}
Android Custom Components