最近学习对话框时发现有两种类型的可供使用,一种是Dialog,另一种则是Android 3.0 引入的基于Fragment的DialogFragment。
从代码的编写角度看,Dialog使用起来要更为简单,但是Google则是推荐尽量使用DialogFragment(对于Android 3.0以下的版本,可以结合使用support包中提供的DialogFragment以及FragmentActivity)。今天试着用这两种方式来创建对话框,发现DialogFragment果然有一个非常好的特性(在手机配置变化,导致Activity需要重新创建时,例如旋屏,基于DialogFragment的对话框将会由FragmentManager自动重建,然而基于Dialog实现的对话框则没有这样的能力)。
下面是两段实例代码:
他们使用的界面都一样:(dialog.xml)
01 |
<? xml version = "1.0" encoding = "utf-8" ?>
|
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
|
03 |
android:layout_width = "match_parent"
|
04 |
android:layout_height = "match_parent"
|
05 |
android:orientation = "vertical" >
|
06 |
07 |
< ImageView
|
08 |
android:layout_width = "wrap_content"
|
09 |
android:layout_height = "wrap_content"
|
10 |
android:src = "@drawable/ic_launcher" />
|
11 |
12 |
</ LinearLayout >
|
1.基于Dialog实现的对话框
01 |
public class MainActivity extends Activity {
|
02 |
private Button clk;
|
03 |
private Dialog dialog;
|
04 |
@Override
|
05 |
protected void onCreate(Bundle savedInstanceState) {
|
06 |
super .onCreate(savedInstanceState);
|
07 |
setContentView(R.layout.activity_main);
|
08 |
|
09 |
clk = (Button) findViewById(R.id.clk);
|
10 |
dialog = new Dialog( this );
|
11 |
dialog.setContentView(R.layout.dialog);
|
12 |
clk.setOnClickListener( new OnClickListener() {
|
13 |
|
14 |
@Override
|
15 |
public void onClick(View v) {
|
16 |
dialog.show();
|
17 |
}
|
18 |
});
|
19 |
}
|
20 |
} |
除此之外,其实还有一个问题,就是在logcat中会看到异常信息:Android..leaked .. window,这是因为在Activity结束之前,Android要求所有的Dialog必须要关闭。我们旋屏后,Activity会被重建,而上面的代码逻辑并没有考虑到对话框的状态以及是否已关闭。
于是将上述代码修改为:
01 |
public class MainActivity extends Activity {
|
02 |
private Button clk;
|
03 |
private Dialog dialog;
|
04 |
@Override
|
05 |
protected void onCreate(Bundle savedInstanceState) {
|
06 |
super .onCreate(savedInstanceState);
|
07 |
setContentView(R.layout.activity_main);
|
08 |
|
09 |
clk = (Button) findViewById(R.id.clk);
|
10 |
dialog = new Dialog( this );
|
11 |
dialog.setContentView(R.layout.dialog);
|
12 |
clk.setOnClickListener( new OnClickListener() {
|
13 |
|
14 |
@Override
|
15 |
public void onClick(View v) {
|
16 |
dialog.show();
|
17 |
}
|
18 |
});
|
19 |
20 |
//用户恢复对话框的状态
|
21 |
if (savedInstanceState != null && savedInstanceState.getBoolean( "dialog_show" ))
|
22 |
clk.performClick();
|
23 |
}
|
24 |
25 |
/**
|
26 |
* 用于保存对话框的状态以便恢复
|
27 |
*/
|
28 |
@Override
|
29 |
protected void onSaveInstanceState(Bundle outState) {
|
30 |
super .onSaveInstanceState(outState);
|
31 |
if (dialog != null && dialog.isShowing())
|
32 |
outState.putBoolean( "dialog_show" , true );
|
33 |
else
|
34 |
outState.putBoolean( "dialog_show" , false );
|
35 |
}
|
36 |
37 |
/**
|
38 |
* 在Activity销毁之前,确保对话框以关闭
|
39 |
*/
|
40 |
@Override
|
41 |
protected void onDestroy() {
|
42 |
super .onDestroy();
|
43 |
if (dialog != null && dialog.isShowing())
|
44 |
dialog.dismiss();
|
45 |
}
|
46 |
} |
2. 基于DialogFragment的对话框
与上面的对话框使用同样的界面布局,此处仅仅展现一个简单对话框,因此只重写了onCreateView方法
1 |
public class MyDialogFragment extends DialogFragment {
|
2 |
@Override
|
3 |
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
4 |
Bundle savedInstanceState) {
|
5 |
View v = inflater.inflate(R.layout.dialog, container, false );
|
6 |
return v;
|
7 |
}
|
8 |
} |
01 |
public class MainActivity extends FragmentActivity {
|
02 |
private Button clk;
|
03 |
@Override
|
04 |
protected void onCreate(Bundle savedInstanceState) {
|
05 |
super .onCreate(savedInstanceState);
|
06 |
setContentView(R.layout.activity_main);
|
07 |
|
08 |
clk = (Button) findViewById(R.id.clk);
|
09 |
clk.setOnClickListener( new OnClickListener() {
|
10 |
|
11 |
@Override
|
12 |
public void onClick(View v) {
|
13 |
MyDialogFragment mdf = new MyDialogFragment();
|
14 |
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
15 |
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
|
16 |
mdf.show(ft, "df" );
|
17 |
}
|
18 |
});
|
19 |
}
|
20 |
} |
其实DialogFragment还拥有fragment的优点,即可以在一个Activity内部实现回退(因为FragmentManager会管理一个回退栈)