1.Dialog对话框:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
public
class
MainActivity
extends
Activity
implements
OnClickListener
{
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(
this
);
findViewById(R.id.button2).setOnClickListener(
this
);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
@Override
public
void
onClick(View v)
{
switch
(v.getId())
{
case
R.id.button1:
btn1Click();
break
;
case
R.id.button2:
btn2Click();
break
;
default
:
break
;
}
}
private
void
btn1Click()
{
AlertDialog.Builder builder =
new
AlertDialog.Builder(
this
);
// 设置对话框标题、内容、按钮,set方法每次返回this,即dialog本身
builder.setIcon(R.drawable.ic_launcher);
//设置标题图片
builder.setTitle(
"对话框标题"
);
builder.setMessage(
"对话框内容"
);
builder.setPositiveButton(
"关闭"
,
null
);
// 系统只提供三个对话框按钮,区别是默认的显示位置,Neutral在中间
builder.setNegativeButton(
"确定"
,
new
DialogInterface.OnClickListener()
// 此处的listener与上面的按钮listener来自于不同包
{
@Override
public
void
onClick(DialogInterface dialog,
int
which)
{
Log.e(
"duihuakuang"
,
"点击了对话框按钮"
);
}
});
// builder.setNeutralButton("应用", listener);
AlertDialog dialog = builder.create();
dialog.show();
//记得加上show()方法
}
// 另一种写法,复用创建对象,模板方法设计模式
private
void
btn2Click()
{
showDialog(
0
);
//showDialog方法最终实现了onCreateDialog(0)方法
}
//重写onCreateDialog方法,避免重复创建对象
@Override
protected
Dialog onCreateDialog(
int
id)
{
return
new
AlertDialog.Builder(
this
).setIcon(R.drawable.ic_launcher)
.setTitle(
"对话框标题"
).setMessage(
"对话框内容"
).setPositiveButton(
"关闭"
,
null
)
.setNegativeButton(
"确定"
,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface dialog,
int
which)
{
Log.e(
"duihuakuang"
,
"点击了对话框按钮"
);
}
}).setNeutralButton(
"应用"
,
null
).create();
// return super.onCreateDialog(id);
}
}
|
2.定制dialog方法:
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
|
@Override
public
void
onClick(View v)
{
if
(v.getId() == R.id.button1)
{
showDialog(
1
);
}
if
(v.getId() == R.id.button2)
{
showDialog(
2
);
}
}
@Override
@Deprecated
protected
Dialog onCreateDialog(
int
id)
{
if
(id ==
1
)
//全定制,使用自定义布局
{
final
Dialog dialog =
new
Dialog(
this
);
dialog.setContentView(R.layout.dialog_layout);
dialog.findViewById(R.id.button_dialog).setOnClickListener(
new
OnClickListener()
{
@Override
public
void
onClick(View v)
{
Toast.makeText(MainActivity.
this
,
"定制对话框"
, Toast.LENGTH_SHORT).show();
dialog.dismiss();
// 关闭对话框
}
});
return
dialog;
}
if
(id ==
2
)
//半定制,只修改中间布局
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.notify_layout,
null
);
return
new
AlertDialog.Builder(
this
).setTitle(
"半定制对话框"
).setView(view).setPositiveButton(
"退出"
,
null
).create();
}
return
null
;
}
|
3.定制的dialog去掉标题栏和背景色等:
1
2
3
4
5
6
7
8
9
|
<style name=
"myDialogTheme"
parent=
"android:Theme.Dialog"
>
<item name=
"android:windowFrame"
>
@null
</item>
<item name=
"android:windowIsFloating"
>
true
</item>
<item name=
"android:windowIsTranslucent"
>
false
</item>
<item name=
"android:windowNoTitle"
>
true
</item><!--除去title-->
<item name=
"android:windowContentOverlay"
>
@null
</item>
<item name=
"android:backgroundDimEnabled"
>
false
</item>
<item name=
"android:windowBackground"
>
@drawable
/ic_touming</item><!--除去背景色,也可以
@null
-->
</style>
|
然后:
1
|
Dialog dialog =
new
Dialog(
this
,R.style.myDialogTheme);
|
4.activity与dialog对话框之间的交互:
打开对话框:
1
2
3
|
Bundle bundle =
new
Bundle();
bundle.putString(HBContant.KEY_BUNDLE_DIALOG, mNotifyStr);
showDialog(
1
, bundle);
|
对话框处理:
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
|
// 创建注册填写错误提示对话框
@Override
@Deprecated
protected
Dialog onCreateDialog(
int
id)
{
final
Dialog dialog =
new
Dialog(
this
, R.style.customDialogTheme);
if
(id ==
1
)
{
// 获取activity传送过来的值
dialog.setContentView(R.layout.dialog_singlebtn);
dialog.findViewById(R.id.dialog_ok).setOnClickListener(
new
OnClickListener()
{
@Override
public
void
onClick(View v)
{
dialog.dismiss();
// 关闭对话框
}
});
}
return
dialog;
}
@Override
@Deprecated
//每次弹出对话框时被回调以动态更新对话框内容的方法
protected
void
onPrepareDialog(
int
id, Dialog dialog, Bundle args)
{
super
.onPrepareDialog(id, dialog, args);
if
(id ==
1
)
{
String str = args.getString(HBContant.KEY_BUNDLE_DIALOG);
TextView dialog_content = (TextView) dialog.findViewById(R.id.dialog_text);
dialog_content.setText(str);
}
}
|
5.设置对话框的位置和大小
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
|
/**
* 初始化查询对话框
*/
private
void
InitSearchDialog()
{
View searchDialogLayout = getLayoutInflater().inflate(R.layout.handmaterial_productsearch_window,
null
);
View mBtn_search = searchDialogLayout.findViewById(R.id.productsearch_btn_search);
mBtn_search.setOnClickListener(
this
);
//创建查询窗口
AlertDialog.Builder builder =
new
AlertDialog.Builder(
this
);
mSearchDialog = builder.setIcon(R.drawable.ic_launcher).setTitle(
"查询"
).setView(searchDialogLayout).setNegativeButton(
"取消"
,
null
)
.setPositiveButton(
"保存"
,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface dialog,
int
which)
{
dialog =
null
;
}
}).create();
}
/**
* 显示查询对话框
*/
private
void
showSearchDialog()
{
mSearchDialog.show();
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
//为获取屏幕宽、高
android.view.WindowManager.LayoutParams lp = mSearchDialog.getWindow().getAttributes();
//获取对话框当前的参数值
lp.height = (
int
) (d.getHeight() *
0.8
);
//高度设置为屏幕的0.8
lp.width = (
int
) (d.getWidth() *
0.8
);
//宽度设置为屏幕的0.8
mSearchDialog.getWindow().setAttributes(lp);
//设置生效
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1199508,如需转载请自行联系原作者