1.多个Activity共用相同布局或者相同控件:避免重复代码
BaseActivit代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
public
class
BaseActivity
extends
Activity
implements
OnClickListener
{
/*
* 多个Activity共用相同布局或者相同控件
* 写个基类继承,避免重复代码
*/
@Override
public
void
onClick(View v)
{
switch
(v.getId())
{
case
R.id.common_titlebar_btn_back:
finish();
break
;
default
:
break
;
}
baseOnClick(v);
}
protected
void
baseOnClick(View v)
{
}
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
// setContentView(R.layout.activity_base);
}
@Override
protected
void
onResume()
{
super
.onResume();
onPostOnCreate();
}
public
final
void
onPostOnCreate()
//find同一个控件
{
findViewById(R.id.common_titlebar_btn_back).setOnClickListener(
this
);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.base, menu);
return
true
;
}
}
|
子类代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
class
ThirdActivity
extends
BaseActivity
{
/**
* layout里复用同一布局:
* <include
* android:id="@+id/include1"
* layout="@layout/common_titlebar" />
*/
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_third_main);
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1224627,如需转载请自行联系原作者