BaseActivity的好处
1.处理共同操作
2.代码规范
3.可以提供常用变量方法
/**
* Created by Administrator on 2018/1/25 0025.
*/
public abstract class BaseActicity extends FragmentActivity implements View.OnClickListener
{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutresID());
View viewRoot = findViewById(android.R.id.content);//找到根View
setButtonOnClickListener(viewRoot);
//Button btn =findView(R.id.btn_back);
initView();
initListener();
initData();
}
/**
* 使用泛型,每次findViewByID不用强转、
* @param id
* @param <T>
* @return
*/
public <T> T findView(int id){
T view = (T)findViewById(id);
return view;
}
//为界面每一个按钮添加点击事件
private void setButtonOnClickListener(View view) {
if(view instanceof Button || view instanceof ImageButton){
view.setOnClickListener(this);
}else if( view instanceof ViewGroup){
ViewGroup vg =(ViewGroup)view;
for (int i=0 ;i<vg.getChildCount();i++){
View v= vg.getChildAt(i);
setButtonOnClickListener(v);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_back:
//单击得是返回按钮
finish();
break;
default:
//默认使用子类处理
onClick(v,v.getId());
break;
}
}
/**
* 打印吐司
* @param text
*/
public void showToast(String text){
Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
//在容器中间显示
toast.setGravity(Gravity.CENTER ,0,0);
toast.show();
}
/**
* @param v
* @param id
*/
protected abstract void onClick(View v, int id);
protected abstract void initListener();
protected abstract void initView();
/**
* 设置布局
* @return layoutID
*/
protected abstract int getLayoutresID();
protected abstract void initData();
}
2.抽取出接口和Utils
UIoperation.java
public interface UIoperation extends View.OnClickListener {
void initListener();
void initView();
int getLayoutresID();
void initData();
void onClick(View v, int id);
}
Utils.java
public class Utils {
//为界面每一个按钮添加点击事件
public static void setButtonOnClickListener(View view , View.OnClickListener listener) {
if(view instanceof Button || view instanceof ImageButton){
view.setOnClickListener(listener);
}else if( view instanceof ViewGroup){
ViewGroup vg =(ViewGroup)view;
for (int i=0 ;i<vg.getChildCount();i++){
View v= vg.getChildAt(i);
setButtonOnClickListener(v,listener);
}
}
}
public static void showToasts(Context context,String text){
Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
//在容器中间显示
toast.setGravity(Gravity.CENTER ,0,0);
toast.show();
}
}
BaseActivity.java
public abstract class BaseActicity extends FragmentActivity implements UIoperation
{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
setContentView(getLayoutresID());
View viewRoot = findViewById(android.R.id.content);//找到根View
Utils.setButtonOnClickListener(viewRoot,this);
//Button btn =findView(R.id.btn_back);
initView();
initListener();
initData();
System.out.println("AAAAAAAA");
}
/**
* 使用泛型,每次findViewByID不用强转、
* @param id
* @param <T>
* @return
*/
public <T> T findView(int id){
T view = (T)findViewById(id);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_back:
//单击得是返回按钮
finish();
break;
default:
//默认使用子类处理
onClick(v,v.getId());
break;
}
}
/**
* 打印吐司
* @param text
*/
public void showToast(String text){
Utils.showToasts(this,text);
}
}