Customize your own baseactivity for your Android app

Source: Internet
Author: User
I believe everyone is developing Android I must have encountered a lot of repetitive work in the application process. , RepeatedCode , Sometimes the same is true for layout files. , Repetitive work required , In this caseProgramIt must be a very tiring and tedious task. , So when we write code, do we need to consider letting us write less code? , Programmers must learn to be lazy. , Otherwise ........

 

When developing applications, our design is actually a Unified overall style. , Then we can write some common code. , This facilitates subsequent maintenance for the program. , No more nonsense. , I believe everyone understands this. , Today, what I share with you is to customize your own Baseactivity, This Baseactivity It encapsulates some common code. , For example, the titles and buttons in the development process must have , So we can write all these public items in this Baseactivity Li , Other functions Activity This will be inherited later Baseactivity. First

Read,Are you inspired or knowledgeable??ThenBaseactivityCore code in:

/*** Inherited from activity for future convenient management ** @ author coder **/public class baseactivity extends activity {private view titleview; private textview TV _title; private button btn_left, btn_right; private linearlayout ly_content; // layout of the content area private view contentview; @ override protected void oncreate (bundle savedinstancestate) {// todo auto-generated method stub super. oncreate (savedinstancestate); setcontentview (R. Layout. common_title); titleview = findviewbyid (R. id. titleview); TV _title = (textview) titleview. findviewbyid (R. id. TV _title); btn_left = (button) titleview. findviewbyid (R. id. btn_left); btn_right = (button) titleview. findviewbyid (R. id. btn_right); ly_content = (linearlayout) findviewbyid (R. id. ly_content);}/***** set the content area ** @ Param resid * resource file ID */Public void setcontentlayout (INT resid) {layoutinf Later Inflater = (layoutinflater) getsystemservice (context. layout_inflater_service); contentview = Inflater. inflate (resid, null); layoutparams = new layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); contentview. setlayoutparams (layoutparams); contentview. setbackgrounddrawable (null); If (null! = Ly_content) {ly_content.addview (contentview) ;}}/***** set the content area ** @ Param view * view object */Public void setcontentlayout (view) {If (null! = Ly_content) {ly_content.addview (View) ;}/ *** view of the obtained content ** @ return */public view getlycontentview () {return contentview ;} /*** get the left button ** @ return */Public button getbtn_left () {return btn_left ;} /*** get the button on the right ** @ return */Public button getbtn_right () {return btn_right ;} /*** set the title ** @ Param title */Public void settitle (String title) {If (null! = TV _title) {TV _title.settext (title) ;}/ *** set the title ** @ Param resid */Public void settitle (INT resid) {TV _title.settext (getstring (resid);}/*** set the image resource of the left button ** @ Param resid */Public void setbtn_leftres (INT resid) {If (null! = Btn_left) {btn_left.setbackgroundresource (resid);}/*** set the image resource of the left button ** @ Param BM */Public void setbtn_leftres (drawable) {If (null! = Btn_left) {btn_left.setbackgrounddrawable (drawable);}/*** set the image resource of the right button ** @ Param resid */Public void setbtn_rightres (INT resid) {If (null! = Btn_right) {btn_right.setbackgroundresource (resid);}/*** set the image resource of the right button ** @ Param drawable */Public void setbtn_rightres (drawable) {If (null! = Btn_right) {btn_right.setbackgrounddrawable (drawable) ;}/ *** hide the title bar above */Public void hidetitleview () {If (null! = Titleview) {titleview. setvisibility (view. Gone) ;}}/*** hide the button on the left */Public void hidebtn_left () {If (null! = Btn_left) {btn_left.setvisibility (view. Gone) ;}/ ***** hide the button on the right */Public void hidebtn_right () {If (null! = Btn_right) {btn_right.setvisibility (view. Gone) ;}} public baseactivity (){}}

Next, let's give a usage.:

 
Public class twobtnactivity extends baseactivity {@ override protected void oncreate (bundle savedinstancestate) {// todo auto-generated method stub super. oncreate (savedinstancestate); setcontentlayout (R. layout. two); // set the title settitle ("two buttons"); // Add the listener event getbtn_left () for the button on the left (). setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {onbackpressed ();}});}}

Okay, you're done.,This omnipotentBaseactivityIs it easy to use?,I hope this small sharing will be helpful to everyone.

If you are interested in Android development, join the group for discussion ~ 252742977 (Shanghai) 253604146 (Xiamen)

 

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.