Android Custom Components

Source: Internet
Author: User

[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

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.