Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47101387
Children's shoes with certain Android development experience know that sometimes Android provides native views that do not meet the business needs of our own projects, which requires us to customize the Android control ourselves, and in many cases we need to go to a lot of controls (Android native controls, custom controls) to create a new view to meet the business needs of our own projects. Well, this article is about how to customize the Android composite control, and in this article, I combine a text box and a button into a composite control to show you how to implement the functionality of a custom composite control in Android. In this example, click the button to clear the text box data function, OK, let's go straight to the topic.
First, the principle
A composite control is an indivisible, self-contained attempt group that contains multiple sub-views that are arranged and joined together. When you create a composite control, you must define the layout, appearance, and interaction of the views it contains. When a composite control is created by extending a viewgroup (usually a layout). Therefore, to create a new composite control, you first select a layout class that best fits the child control, and then extend the class.
II. implementation 1, creating a layout file
Create a layout file in the Res/layout directory fuhe_item.xml, this file is simple, first arranged in a linear layout, above a text box, the text box below a button control.
The specific implementation code is as follows:
<?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=" wrap_content " android:o rientation= "vertical" > <edittext android:id= "@+id/edittext" android:layout_width= "Match_ Parent " android:layout_height=" wrap_content " android:inputtype=" text "/><button android:id=" @ +id/clearbutton " android:layout_width=" match_parent " android:layout_height=" Wrap_content " android:text= "Clear"/></linearlayout>
2. Create a custom control class Clearableedittext
This class inherits from LinearLayout, and in this class references the layout file above, we rewrite the constructor of this class and populate the layout resource with the inflate method in the Layoutinflater system service. The inflate method can receive a layout resource and then return a view that has already been populated.
For some special cases, for example, the returned view should be the class being created, you can pass in a parent view, and then automatically append the result to it.
In our class, we populate the layout resource in the construction method, get the text box and button control it contains, and call the Hookupbutton method, implement the function of clicking the button to clear the text box data and prompting the user "text box data is clear" in the Hookupbutton method.
The specific implementation is shown in the code:
Package Com.lyz.fuhe.activity;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.widget.button;import Android.widget.edittext;import android.widget.LinearLayout; Import android.widget.toast;/** * Custom composite layout * @author Liuyazhuang * */public class Clearableedittext extends LinearLayout {/ /text box private EditText edittext;//button Private button clearbutton;public Clearableedittext (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.fuhe_item, this, true); This.edittext = (EditText) Findviewbyid (R.id.edittext); This.clearbutton = (Button) Findviewbyid (R.id.clearbutton); Hookupbutton (context);} /** * Button handler */private void Hookupbutton (Final context context) {This.clearButton.setOnClickListener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {edittext.settext (""); Toast.maketext (Context, "text box data cleared", Toast.)Length_short). Show ();}});}}
3, modify the Mainactivity class
The modification of this class is simple, and we only need to modify the view set by the original Setcontentview () method to our own defined Clearableedittext object in the OnCreate () method.
The specific implementation code is as follows:
Package Com.lyz.fuhe.activity;import Android.os.bundle;import Android.app.activity;import android.view.Menu;/** * Program Entry Mainactivity * @author Liuyazhuang * */public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bu Ndle savedinstancestate) {super.oncreate (savedinstancestate);//setcontentview (r.layout.activity_main);// Create Clearableedittext Instance object Clearableedittext editText = new Clearableedittext (this);// Set the displayed view to Clearableedittext object Setcontentview (EditText);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
4, Androidmanifest.xml
In this example, there is no need to provide any other permission class implementations, and the contents of the Androidmanifest.xml file are automatically generated by Android.
The specific implementation code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android " package=" com.lyz.fuhe.activity " android:versioncode=" 1 " android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application" android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= " Com.lyz.fuhe.activity.MainActivity " android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= " Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </ Application></manifest>
Third, the Operation effect
Program run
text box input data
Click the button to clear the text box data
Four, warm tips
In this example, for the sake of the aspect, I write some text directly in the layout file and the related class, Everyone in the real project to write these words in the String.xml file, in the external reference to these resources, remember, this is the most basic development knowledge and specifications as an Android programmer, I am here just to facilitate directly written in the class and layout files.
You can get full Android custom composite Control source code to http://download.csdn.net/detail/l1028386804/8938835 link
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android--Implementation of custom composite controls