In order to allow the code to be reused more, the combined control is used. Here are the methods used in the project I am writing.
1. First write the required controls to be combined and encapsulate them in a layout XML layout file.
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "68dip"Android:id= "@+id/aaa" > <TextViewAndroid:id= "@+id/tv_update_title"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:textsize= "20SP"Android:layout_margintop= "10DP"Android:layout_marginleft= "10DP"Android:text= "Upgrade" /> <TextViewAndroid:layout_below= "@id/tv_update_title"Android:id= "@+id/tv_update_content"android:textsize= "15SP"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margintop= "10DP"Android:layout_marginleft= "10DP"Android:text= "Stop Update" /> <CheckBoxandroid:checked= "false"Android:id= "@+id/cb_isupdate"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"android:layout_centervertical= "true" /> </Relativelayout>
2. Custom Java Classes
PackageCom.frank.mobilesafe.ui;ImportCOM.FRANK.MOBILESAFE.R;ImportAndroid. R.bool;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.CheckBox;Importandroid.widget.RelativeLayout;ImportAndroid.widget.TextView; Public classSettingitemviewextendsRelativelayout {PrivateCheckBox cb_update; PrivateTextView Tv_update_title; PrivateTextView tv_update_content; PublicSettingitemview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); Initview (context); } Private voidInitview (Context context) {//TODO auto-generated Method StubView.inflate (context, R.layout.setting_item_view, This); Cb_update=(CheckBox) Findviewbyid (r.id.cb_isupdate); Tv_update_title=(TextView) Findviewbyid (r.id.tv_update_title); Tv_update_content=(TextView) Findviewbyid (r.id.tv_update_content); } PublicSettingitemview (Context context, AttributeSet attrs) {Super(context, attrs); Initview (context); } PublicSettingitemview (Context context) {Super(context); Initview (context); } /*** Check if * is selected *@return */ Public BooleanisChecked () {returncb_update.ischecked (); } /*** Set the status of the combined control *@paramisChecked*/ Public voidSetchecked (BooleanisChecked) {cb_update.setchecked (isChecked); } /*** Set Description information *@paramisChecked*/ Public voidSetdesc (String text) {tv_update_content.settext (text); }}3. Referencing in the main interface
<?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= "vertical" > <TextViewAndroid:id= "@+id/tv_maintitle"Android:layout_width= "Match_parent"Android:layout_height= "55DP"Android:background= "#8866ff00"android:gravity= "Center"Android:text= "Set Center"android:textsize= "22SP" /> <Com.frank.mobilesafe.ui.SettingItemViewAndroid:id= "@+id/siv_update"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /></LinearLayout>
4. Main interface Call
Public classSettingactivityextendsActivity {PrivateSettingitemview siv_update; Privatesharedpreferences sp_update; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_setting); Siv_update=(Settingitemview) Findviewbyid (r.id.siv_update); Sp_update= getsharedpreferences ("config", mode_private); BooleanUpdate = Sp_update.getboolean ("Update",false); if(update) {siv_update. Setchecked (true); Siv_update. Setdesc ("New version is updated"); } Else{siv_update. Setchecked (false); Siv_update. Setdesc ("Stop Updating"); } siv_update.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Editor Editor=Sp_update.edit (); //TODO auto-generated Method Stub if(siv_update.ischecked ()) {siv_update. Setchecked (false); Siv_update. Setdesc ("Stop Updating"); Editor.putboolean ("Update",false); } Else{siv_update. Setchecked (true); Siv_update. Setdesc ("New version is updated"); Editor.putboolean ("Update",true); } } }); } }5. Complete
Normal display
Android Development Learning Note-Custom combo controls