Android中BaseActivity的用法

来源:互联网 发布:js 点击按钮刷新div 编辑:程序博客网 时间:2024/05/22 13:17

为什么要用BaseActivity?
作为一名懒程序猿,自然是尽可能对一些具有通用性的东西写成一个函数,避免每次使用都写很长很长的代码.这里不在叙述什么事面向对象编程这些深奥的道理,只贴一下我自己习惯用的一些方法.
用法:使用泛型写一个自己的findByViewId(), 好处就是在查找控件的时候不需要自己去强制转换, 由程序帮你自动转换.

其实函数名是随便取的,但是我习惯这样写.

@SuppressWarnings("unchecked")protected <T extends View> T FindViewById(int rsId) {    return (T) findViewById(rsId);}

在其他的Activity里继承BaseActivity即可使用此方法.你会惊奇的发现,IDE不会提示你类型不符合了.这就是泛型的强大.相信看了上面那一段代码之后,你已经明白了一些,其实我们可以把一些在Activity里高频出现的代码定义在BaseActivity里.然后就可以偷懒.
下面再贴几个常用函数:

/***有没有觉得Toast很长很长...每次都要敲好长一串!*这不,这样就可以偷懒了...*/public void Toast(String str)    {        android.widget.Toast.makeText(this, str, android.widget.Toast.LENGTH_SHORT).show();    }/***这个方法其实并不依赖于Activity,完全可以在某个类里写一个静态函数,但是静态函数需要写类名.getText()...这样直接写getText(tv)就可以达到想要的结果*/private String getText(TextView tv){    if(tv==null)    return "";    return tv.getText().toString();}//在子类的一些接口方法里面this失效了...//己又不愿意写那么长的类名.this...//所以在基类定义一个_this的变量..//然后就可以到处用了,唉 真是懒.//当然你还可以在OnCreate里面进行更多的操作.public Activity _this;    @Overrideprotected void onCreate(Bundle savedInstanceState) {        // TODO 自动生成的方法存根        super.onCreate(savedInstanceState);        _this=this;}

这些都是我常用的方法.BaseActivity还有一些进阶的用法.下面是利用BaseActivity来实现通用标题栏的.下面这是效果图.这里写图片描述

抱歉这是一个商业项目,不能给出太大的截图 但是我又太懒不想去写demo.下面先贴标题栏的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout     android:layout_width="match_parent"    android:layout_height="50dp"    android:id="@+id/ll"     xmlns:android="http://schemas.android.com/apk/res/android"><RelativeLayout     android:layout_width="match_parent"    android:layout_height="50dp" >    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/backcolor"        android:scaleType="fitXY" />    <ImageView        android:id="@+id/left_button"        android:layout_width="40dp"        android:layout_height="wrap_content"        android:layout_centerHorizontal="false"        android:layout_centerVertical="true"        android:layout_marginLeft="10dp"        android:src="@drawable/common_jiantou" />    <TextView        android:id="@+id/title_view"        android:text="懒是一种态度"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="false"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:textColor="@android:color/white"        android:textSize="16sp" />        <TextView        android:id="@+id/right_button"        android:layout_marginRight="10dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:text="保存"        android:visibility="visible"        android:layout_centerVertical="true"        android:textColor="@android:color/white"        android:textSize="16sp" /></RelativeLayout></LinearLayout>

上面这段代码看起来应该没有困难..BaseActivity代码如下:

private ImageView left_btn;public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉自带的标题栏       setContentView(R.layout.bar); //设置标题栏布局       left_btn=FindViewById(R.id.left_button);       //设置左边按钮的默认点击事件就是关闭当前Activity       left_btn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO 自动生成的方法存根                _this.finish();            }        });    }     /**    *重写一个新增View的视图 子类设置layout的时候调用该方法即可    */public void SetContentView(int layoutResId) {        LinearLayout llContent = (LinearLayout) findViewById(R.id.llContent1);        LayoutInflater inflater = LayoutInflater.from(_this);       View v = inflater.inflate(layoutResId, null);        llContent.addView(v);     } //当然你还可以在里面写一些标题栏控件的操作方法,供子Activity调用,例如修改标题栏文字 以及显示隐藏按钮,重定义点击事件等...   
3 0
原创粉丝点击