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
- Android中BaseActivity的用法
- Android中BaseActivity的使用
- Android 继承BaseActivity的典型用法
- 关于BaseActivity的用法
- android中定义BaseActivity
- android中定义BaseActivity
- Android中定义BaseActivity
- Android 的BaseActivity
- Android 基类BaseActivity的封装
- Android 基类BaseActivity的封装
- android BaseActivity
- android开发中设置基础类BaseActivity
- Android的BaseActivity的设计问题
- Android中基类BaseActivity的设计与实现
- Android MainActivity如何加载BaseActivity的布局
- 通用的BaseActivity--android技术储备
- android BaseActivity与BaseFragment的封装
- Android结合ButterKnife创建自己的BaseActivity
- Minimum Cut(2015年吉林网络赛)
- 用两个栈实现队列
- java 类加载器
- Group Anagram
- 如何查看别人的微信公众号的粉丝数
- Android中BaseActivity的用法
- 打开Voice Over时,CATextLayer的string对象兼容NSString和NSAttributedString导致的Crash(二解决思路2)
- 全文检索神器个人修改版
- 常用的Linux命令
- AS边学边写——day02helloworld跑起来
- 天天学设计模式3--开放封闭原则
- IOS开发之免费证书+不越狱真机调试
- HDU5445 Fang Fang
- 黑盒、白盒、灰盒测试的区别