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
|
// 重写方法,让他返回Dialog。
// 以下是优化代码的替换,只创建一次,每次点击使用showDialog()的方法,不用每次都创建新的,可以减少内存使用
@Override
protected
Dialog onCreateDialog(
int
id)
{
// 因为返回值都是builder,所以可以直接使用.set的方法
return
new
AlertDialog.Builder(MainActivity.
this
).setTitle(
"dialog"
)
.setMessage(
"对话框内容"
)
// dialog里面设置三个按钮,可以关闭对话框
.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface dialog,
int
which)
{
}
})
.setNegativeButton(
"取消"
,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface dialog,
int
which)
{
}
}).setNeutralButton(
"应用"
,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface dialog,
int
which)
{
}
}).create();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Override
@Deprecated
protected
Dialog onCreateDialog(
int
id)
{
final
Dialog dialog =
new
Dialog(
this
);
//设置自定义dialog
dialog.setContentView(R.layout.dialog);
dialog.findViewById(R.id.button2).setOnClickListener(
new
OnClickListener()
{
@Override
public
void
onClick(View v)
{
Toast.makeText(MainActivity.
this
,
"定制"
, Toast.LENGTH_LONG).show();
//关闭对话框
dialog.dismiss();
}
});
return
dialog;
}
|