為你的Android應用定製屬於你的BaseActivity

來源:互聯網
上載者:User
相信大家在開發Android應用的過程中肯定碰到過很多重複的工作,寫著重複的代碼,有時候連布局檔案也是一樣,需要重複的勞動,那麼這樣對於我們程式來講肯定是很累很繁瑣的一件事,所以我們在寫代碼的時候是否需要去考慮讓我們寫更少的代碼,程式員要學會偷懶,否則……..

 

在開發應用程式的時候我們的設計其實整體的樣式是統一,那麼我們就可以寫一些公用的代碼,這樣對程式來講也便於後面的維護,廢話也不多說了,相信大家肯定也懂的,今天我分享給大家的就是定製一個屬於自己的BaseActivity,這個BaseActivity主要封裝了一些公用的代碼,例如我們在開發過程中上面的那些標題和按鈕肯定都要有的,所以我們可以把這些公用的都寫在這個BaseActivity裡,其他功能的Activity以後都繼承這個BaseActivity. 先上

看了,大家是否有所啟發或是有所瞭解呢?那麼接下來就放BaseActivity裡的核心代碼咯:

/** * 繼承於Activity用於以後方便管理 *  * @author coder *  */public class BaseActivity extends Activity {        private View titleView;        private TextView tv_title;        private Button btn_left, btn_right;        private LinearLayout ly_content;        // 內容地區的布局        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);        }        /***         * 設定內容地區         *          * @param resId         *            資源檔ID         */        public void setContentLayout(int resId) {                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);                contentView = inflater.inflate(resId, null);                LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,                                LayoutParams.FILL_PARENT);                contentView.setLayoutParams(layoutParams);                contentView.setBackgroundDrawable(null);                if (null != ly_content) {                        ly_content.addView(contentView);                }        }        /***         * 設定內容地區         *          * @param view         *            View對象         */        public void setContentLayout(View view) {                if (null != ly_content) {                        ly_content.addView(view);                }        }        /**         * 得到內容的View         *          * @return         */        public View getLyContentView() {                return contentView;        }        /**         * 得到左邊的按鈕         *          * @return         */        public Button getbtn_left() {                return btn_left;        }        /**         * 得到右邊的按鈕         *          * @return         */        public Button getbtn_right() {                return btn_right;        }        /**         * 設定標題         *          * @param title         */        public void setTitle(String title) {                if (null != tv_title) {                        tv_title.setText(title);                }        }        /**         * 設定標題         *          * @param resId         */        public void setTitle(int resId) {                tv_title.setText(getString(resId));        }        /**         * 設定左邊按鈕的圖片資源         *          * @param resId         */        public void setbtn_leftRes(int resId) {                if (null != btn_left) {                        btn_left.setBackgroundResource(resId);                }        }        /**         * 設定左邊按鈕的圖片資源         *          * @param bm         */        public void setbtn_leftRes(Drawable drawable) {                if (null != btn_left) {                        btn_left.setBackgroundDrawable(drawable);                }        }        /**         * 設定右邊按鈕的圖片資源         *          * @param resId         */        public void setbtn_rightRes(int resId) {                if (null != btn_right) {                        btn_right.setBackgroundResource(resId);                }        }        /**         * 設定右邊按鈕的圖片資源         *          * @param drawable         */        public void setbtn_rightRes(Drawable drawable) {                if (null != btn_right) {                        btn_right.setBackgroundDrawable(drawable);                }        }        /**         * 隱藏上方的標題列         */        public void hideTitleView() {                if (null != titleView) {                        titleView.setVisibility(View.GONE);                }        }        /**         * 隱藏左邊的按鈕         */        public void hidebtn_left() {                if (null != btn_left) {                        btn_left.setVisibility(View.GONE);                }        }        /***         * 隱藏右邊的按鈕         */        public void hidebtn_right() {                if (null != btn_right) {                        btn_right.setVisibility(View.GONE);                }        }        public BaseActivity() {        }}

  接下來再給出其中的一個用法就可以了:

public class TwoBtnActivity extends BaseActivity {        @Override        protected void onCreate(Bundle savedInstanceState) {                // TODO Auto-generated method stub                super.onCreate(savedInstanceState);                setContentLayout(R.layout.two);                //設定標題                setTitle("兩個按鈕");                // 為左邊的按鈕增加監聽事件                getbtn_left().setOnClickListener(new OnClickListener() {                        @Override                        public void onClick(View v) {                                onBackPressed();                        }                });        }}

  好了大功告成了,這個萬能的BaseActivity是不是很好用呀,希望這樣的一個小小的分享能對大家有所協助咯

熱愛Android開發的朋友們,有興趣可以加入群一起交流哈~252742977(上海)253604146(廈門)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.