6.dialog实现退出功能
2015-08-02 12:57
375 查看
今天在学习看代码的时候,发现了有趣的dialog,有2种退出的方法: 第一种:直接退出;第二种:是延迟退出
第一种:代码如下
//dialog实现,直接可以调用dialog此方法
private void dialog()
{
AlertDialog.Builder builder=new Builder(this);
builder.setTitle("温馨提示");
builder.setMessage("确认退出?");
//确定按钮
builder.setPositiveButton("确认",new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO
Auto-generated method stub
dialog.dismiss();
MainActivity.this.finish();
}
});
//取消按钮
builder.setNegativeButton("取消",new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO
Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}
//引进返回键的方法
@Override
public void onBackPressed() {
//dialog
调用,点击返回键 提示是否退出
dialog();
}
直接退出,也可以在按钮的click事件中来实现调用,也可以在返回键里实现调用退出;
第二种:用AlertDialogFragment 来实现延迟退出
void showDialogFragment(int dialogID) {
switch (dialogID) {
case ALERTTAG:
mDialog =
AlertDialogFragment.newInstance();
mDialog.show(getFragmentManager(),
"Alert");
break;
case PROGRESSTAG:
mDialog =
ProgressDialogFragment.newInstance();
mDialog.show(getFragmentManager(),
"Shutdown");
break;
}
}
protected void continueShutdown(boolean
shouldContinue) {
if (shouldContinue) {
mShutdownButton.setEnabled(false);
showDialogFragment(PROGRESSTAG);
finishShutdown();
} else {
mDialog.dismiss();
}
}
private void finishShutdown() {
new Thread(new Runnable()
{
@Override
public void
run() {
try
{
//
Pretend to do something before
//
shutting down
Thread.sleep(5000);
}
catch (InterruptedException e) {
Log.i(TAG,
e.toString());
}
finally {
finish();
}
}
}).start();
}
public static class AlertDialogFragment
extends DialogFragment {
public static
AlertDialogFragment newInstance() {
return new
AlertDialogFragment();
}
@Override
public Dialog
onCreateDialog(Bundle savedInstanceState) {
return new
AlertDialog.Builder(getActivity())
.setMessage("Do
you really want to exit?")
.setCancelable(false)
.setNegativeButton("No",
new
DialogInterface.OnClickListener() {
public
void onClick(DialogInterface dialog,
int
id) {
((AlertDialogActivity)
getActivity())
.continueShutdown(false);
}
})
.setPositiveButton("Yes",
new
DialogInterface.OnClickListener() {
public
void onClick(
final
DialogInterface dialog, int id) {
((AlertDialogActivity)
getActivity())
.continueShutdown(true);
}
}).create();
}
}
public static class ProgressDialogFragment
extends DialogFragment {
public static
ProgressDialogFragment newInstance() {
return new
ProgressDialogFragment();
}
@Override
public Dialog
onCreateDialog(Bundle savedInstanceState) {
final
ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage("Activity
Shutting Down.");
dialog.setIndeterminate(true);
return
dialog;
}
}
直接调用showDialogFragment(ALERTTAG);即可调用上面的方法
第一种:代码如下
//dialog实现,直接可以调用dialog此方法
private void dialog()
{
AlertDialog.Builder builder=new Builder(this);
builder.setTitle("温馨提示");
builder.setMessage("确认退出?");
//确定按钮
builder.setPositiveButton("确认",new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO
Auto-generated method stub
dialog.dismiss();
MainActivity.this.finish();
}
});
//取消按钮
builder.setNegativeButton("取消",new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO
Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}
//引进返回键的方法
@Override
public void onBackPressed() {
//dialog
调用,点击返回键 提示是否退出
dialog();
}
直接退出,也可以在按钮的click事件中来实现调用,也可以在返回键里实现调用退出;
第二种:用AlertDialogFragment 来实现延迟退出
void showDialogFragment(int dialogID) {
switch (dialogID) {
case ALERTTAG:
mDialog =
AlertDialogFragment.newInstance();
mDialog.show(getFragmentManager(),
"Alert");
break;
case PROGRESSTAG:
mDialog =
ProgressDialogFragment.newInstance();
mDialog.show(getFragmentManager(),
"Shutdown");
break;
}
}
protected void continueShutdown(boolean
shouldContinue) {
if (shouldContinue) {
mShutdownButton.setEnabled(false);
showDialogFragment(PROGRESSTAG);
finishShutdown();
} else {
mDialog.dismiss();
}
}
private void finishShutdown() {
new Thread(new Runnable()
{
@Override
public void
run() {
try
{
//
Pretend to do something before
//
shutting down
Thread.sleep(5000);
}
catch (InterruptedException e) {
Log.i(TAG,
e.toString());
}
finally {
finish();
}
}
}).start();
}
public static class AlertDialogFragment
extends DialogFragment {
public static
AlertDialogFragment newInstance() {
return new
AlertDialogFragment();
}
@Override
public Dialog
onCreateDialog(Bundle savedInstanceState) {
return new
AlertDialog.Builder(getActivity())
.setMessage("Do
you really want to exit?")
.setCancelable(false)
.setNegativeButton("No",
new
DialogInterface.OnClickListener() {
public
void onClick(DialogInterface dialog,
int
id) {
((AlertDialogActivity)
getActivity())
.continueShutdown(false);
}
})
.setPositiveButton("Yes",
new
DialogInterface.OnClickListener() {
public
void onClick(
final
DialogInterface dialog, int id) {
((AlertDialogActivity)
getActivity())
.continueShutdown(true);
}
}).create();
}
}
public static class ProgressDialogFragment
extends DialogFragment {
public static
ProgressDialogFragment newInstance() {
return new
ProgressDialogFragment();
}
@Override
public Dialog
onCreateDialog(Bundle savedInstanceState) {
final
ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage("Activity
Shutting Down.");
dialog.setIndeterminate(true);
return
dialog;
}
}
直接调用showDialogFragment(ALERTTAG);即可调用上面的方法
相关文章推荐
- 5.notification实现
- 4.toast实现,直接复制可调用
- 3.dialog实现,直接可复制调用
- 2.AndroidMainfest.xml文件解释
- #第一周1003解题报告#
- Android新建Project项目工程…
- 1.Android常见的按钮监听器实现
- 搭建Spring4+Spring MVC web工程的最佳实践
- 2.Java学习框架(零基础学起)
- 080 Remove Duplicates from Sorted Array II [Leetcode]
- [算法运用研究]图层混合
- 【转】UITableView使用详解
- php-面向对象(5)
- php-面向对象(4)
- 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation
- 增加swap空间
- 笔试题4
- HTML5
- 图论浅析--最短路之Dijkstra
- ubuntu14.04 系统负载监控器