In Android development, our activity will inevitably use a lot of view parts, and for each view we have to Findviewbyid, set up listeners, get the results of user input and so on. If we think carefully, do these trivial operations not have much to do with the logic of this activity? Many of the activity uses a common view combination, can you abstract them into a reusable composite component?
This article is based on the idea that the development of a simple component, for Android novice, can be used as a reference.
Edittextwithlabel Component Class Definition: (Edittextwithlabel.java)
The code is as follows |
Copy Code |
Package com.raysmond.component; Import Android.content.Context; Import Android.util.AttributeSet; Import Android.view.LayoutInflater; Import Android.widget.EditText; Import Android.widget.LinearLayout; Import Android.widget.TextView; public class Edittextwithlabel extends linearlayout{
TextView label; EditText text;
Public Edittextwithlabel (Context Context,attributeset attrs) { Super (context, attrs); SetOrientation (horizontal); Layoutinflater.from. Inflate (R.layout.edit_text_with_label, this, true); Label = (TextView) Findviewbyid (R.id.comp_le_label); Text = (edittext) Findviewbyid (r.id.comp_le_content);
}
public Boolean setsubviewfocus () { return Text.requestfocus (); }
Public EditText Getedittext () { return text; }
Public String getcontent () { Return Text.gettext (). toString (); }
public void SetContent (String str) { Text.settext (str); }
public void SetError (String error) { Text.seterror (Error); }
} |
Layout XML for Edittextwithlabel components (edit_text_with_label.xml)
The code is as follows |
Copy Code |
<?xml version= "1.0" encoding= "Utf-8"?> <merge xmlns:android= "Http://schemas.android.com/apk/res/android" > <textview Android:id= "@+id/comp_le_label" Android:layout_width= "Wrap_content" android:layout_weight= "3" android:layout_height= "Wrap_content" android:paddingright= "5DP" android:text= "Label" Android:textsize= "25SP" android:gravity= "Center"/> <edittext Android:inputtype= "Text" Android:id= "@+id/comp_le_content" Android:layout_width= "Wrap_content" android:layout_weight= "7" android:layout_height= "Wrap_content" Android:textsize= "25SP" > <requestfocus/> </EditText> </merge> |
Using the Edittextwithlabel composite component, the XML of the main activity (Activity_main.xml)
The code is as follows |
Copy Code |
<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" android:paddingbottom= "@dimen/activity_ Vertical_margin " android:paddingleft=" @dimen/activity_horizontal_margin android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= @dimen /activity_vertical_margin " tools:context=". Mainactivity "" <textview Android:layout_width= "Wrap_content" Android:id= "@+id/hello_world" android:layout_height= "Wrap_content" Android:textsize= "28SP" android:text= "input box component with label"/>
<com.raysmond.component.edittextwithlabel Android:id= "@+id/test_component" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_below= "@id/hello_world" /> </RelativeLayout> |
Using the Edittextwithlabel composite component, the main activity class (Mainactivity.java)
The code is as follows |
Copy Code |
Package com.raysmond.activity; Import Com.raysmond.component.EditTextWithLabel; Import COM.RAYSMOND.COMPONENT.R; Import Android.os.Bundle; Import android.app.Activity; public class Mainactivity extends activity { Edittextwithlabel component;
@Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findview ();
}
public void Findview () { Component = (Edittextwithlabel) Findviewbyid (r.id.test_component); Component.setsubviewfocus (); }
} |
Demo: