Android custom Activity base class

Source: Internet
Author: User
Tags getcolor

Android custom Activity base class

When developing an App, we sometimes encounter multiple interfaces that have one thing in common. For example, they all have the same TitleBar and the TitleBar can be used to set the displayed text. The click event on the TitleBar. If you write TitleBar to every Activity, it is very troublesome. At this time, we can rewrite an Activity base class, let other similar activities inherit this class, thus saving a lot of unnecessary repetitive work. Improved efficiency.

 

The two interfaces share the same features. There is a custom TitleBar, which belongs to our custom base class BaseActivity. If we want a class to have this feature, you only need to inherit BaseActivity and add the corresponding layout. It's okay to write all the events.

 

BaseActivity. java

There is an overwritable TitleBar control, which encapsulates the corresponding method to operate on TitleBar. events of this type will be called by TitleBa. For details about the call, refer to the TitleBar implementation.

 

/*** Override the base class of an Activity. A TitleBar is implemented in the base class to display a title * define a Back button at the same time, exit the current Activity ** @ author mingwei **/public abstract class BaseActivity extends Activity {private TitleBar mTitleBar; @ Overrideprotected void onCreate (Bundle savedInstanceState) {requestWindowFeature (Window. FEATURE_NO_TITLE); super. onCreate (savedInstanceState); setTheme (android. r. style. theme_Light_NoTitleBar);} @ Overridepub Lic void setContentView (int layoutResID) {// TODO Auto-generated method stubsuper. setContentView (layoutResID); initBaseView ();} public void initBaseView () {mTitleBar = (TitleBar) findViewById (R. id. base_titlebar);} public void setTitleBarBackText (String text) {// mTitleBar. setBackText (text);} public void setTitleBarTitle (String tite) {if (mTitleBar! = Null) {mTitleBar. setTitle (tite) ;}} public void setTitleBarTitle (int titleId) {if (mTitleBar! = Null) {mTitleBar. setTitle (getString (titleId) ;}} public void setTitleBarTitleDrawable (Drawable drawable) {if (mTitleBar! = Null) {mTitleBar. Events (drawable) ;}// return key event public void finishActivity () {finish () ;}public void setTitleRTBtnVisiable (int visiable) {if (mTitleBar! = Null) {mTitleBar. setRTBtnVisiable (visiable) ;}} public void setTitleRTBtnText (String text) {if (mTitleBar! = Null) {mTitleBar. setRTBtnText (text) ;}} public void setTitleRTBtnText (int textId) {if (mTitleBar! = Null) {mTitleBar. setRTBtnText (getString (textId) ;}} public void setTitleRTBtnFocusable (boolean focusable) {if (mTitleBar! = Null) {mTitleBar. setRTBtnFocusable (focusable) ;}// Title in the middle Click Event public void setCenterClick (boolean bool) {mTitleBar. setTitleClick (bool);} public void onRtBtnClick () {// click Time of the button in the upper left corner of titlebar} public void onCenterClick () {// title click event in the middle of titlebar }}

 

 


Let's take a look at what TitleBar looks like. TitleBar is an encapsulated RelativLayout, which includes a return key, the Title in, and the right button.

Note: In Note 1, what should I do if the container or control in the Activity calls the method or attribute in the Activity?

For example, when I click Back's ImageView to remove Activity finished (), we only need to convert the Context obtained in the container into the corresponding Activity, then you can conveniently call the Activity method.

 

 

/*** The TitleBar used by the base class BaseActivity, return the page and display the Title information of the current page ** @ author mingwei **/public class TitleBar extends RelativeLayout implements OnClickListener {private ImageView mBack; private TextView mTitle; private TextView mRTBtn; baseActivity mContext; public TitleBar (Context context) {this (context, null);} public TitleBar (Context context, AttributeSet attrs) {super (context, attrs, 0 );} public TitleBar (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); mContext = (BaseActivity) getContext (); // Note 1}/*** initialize the control */@ Overrideprotected void onFinishInflate () {super. onFinishInflate (); mContext = (BaseActivity) getContext (); mBack = (ImageView) findViewById (R. id. base_titlebar_back); mTitle = (TextView) findViewById (R. id. base_titlebar_title); mRTBtn = (TextView) findViewById (R. id. base_titlebar_rtbtn); mBack. setOnClickListener (this); mTitle. setOnClickListener (this); mRTBtn. setOnClickListener (this); mTitle. setClickable (false);}/*** check whether the control on the right is visible */public void setRTBtnVisiable (int visiable) {mRTBtn. setVisibility (visiable);}/*** text version of the control on the right */public void setRTBtnText (String title) {mRTBtn. setText (title);}/*** display color change of the right control */public void setRTBtnFocusable (boolean focusable) {mRTBtn. setEnabled (focusable); if (focusable) {mRTBtn. setTextColor (getResources (). getColor (R. color. base_rtbtn_clickable_color);} else {mRTBtn. setTextColor (getResources (). getColor (R. color. base_rtbtn_clickunable_color);}/*** return to the right control */public TextView getRTBtnTextView () {return mRTBtn ;} /*** Click Event of the intermediate control */public void setTitleClick (boolean bool) {mTitle. setClickable (bool);}/*** intermediate control text */public void setTitle (String title) {mTitle. setText (title);}/*** intermediate control icon */public void setCompoundDrawables (Drawable drawable) {mTitle. setCompoundDrawables (null, null, drawable, null);}/*** intermediate control icon */public void setTitleRightDrawable (Drawable drawable) {mTitle. setCompoundDrawablesWithIntrinsicBounds (null, null, drawable, null);}/*** click conditions of the control to call the BaseActivity method respectively, * After the base class is overwritten, these methods will be called in the subclass */@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. base_titlebar_back: mContext. finishActivity (); break; case R. id. base_titlebar_title: mContext. onCenterClick (); break; case R. id. base_titlebar_rtbtn: mContext. onRtBtnClick (); break; default: break ;}}}


 

 

 

 

La s used by TitleBar 

 

 

 
     
      
       
        
     
    
   
  
 

 

 


Write it here. Let's see how to use it.

 

/*** Inherit BaseActivity * @ author mingwei **/public class AllPictureActivity extends BaseActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. allpicture_activity); setTitleBarTitle (R. string. allpicture_title); setTitleRTBtnVisiable (View. VISIBLE); setTitleRTBtnText (R. string. allpicture_ OK); setTitleBarTitleDrawable (getResources (). getDrawable (R. drawable. all_picture_more_dropdown); setCenterClick (true); initView ();} private void initView () {}/ *** click event in the middle of TitleBar */@ Overridepublic void onCenterClick () {super. onCenterClick (); taggleLayout ();} private void taggleLayout () {} private void changeData (int I) {}/*** return button click event */@ Overridepublic void finishActivity () {// TODO Auto-generated method stubsuper. finishActivity ();}/*** click event in the upper-right corner */@ Overridepublic void onRtBtnClick () {super. onRtBtnClick ();}}

 

 

Do not forget to add the TitleBar file reference to the Activity layout file.

 

 
     
  
   
// TitleBar layout File
           
           
  
 

 

 

Do not forget to write your unchanged ids to the ids file.

 

 
     
  
   base_titlebar
  
 


Shows the implementation interface:

 

 

 

 

Related Article

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.