AlertDialog dialog = builder.create();
dialog.show();
}
});
2.全局Dialog
这里所说的全局Dialog是一个应用于优先级非常高的通知,就是当前应用无论是处于哪一个页面上,都需要弹出的Dialog来提示用户一些信息,可以用于紧急的通知和提醒。实现方法也不是很复杂,一般项目中我们都会通过一个类来管理我们的Activity,即AppManager,所以只要能获取到当前显示的Activity就行了,也就是栈顶的Activity,再封装一个BaseActivity类统一管理,给各个Activity继承,然后需要主页面或者主类(mainactivity)处理Dialog的显示问题,通过管理APP(AppManager)获取到当前栈顶的Activity,用于构造Dialog就行了。
/ Activity管理类 @author Donkor /
public class AppManager{
private static Stack<Activity> activityStack;
private static AppManager instance;
private AppManager(){} / 单一实例 */
public static AppManager getAppManager(){
if(instance==null)
{
instance=new AppManager();
}
return instance;
} / 添加Activity到堆栈 /
public void addActivity(Activity activity)
{
if(activityStack==null)
{
activityStack=new Stack<Activity>();
}
activityStack.add(activity);
} /* 获取当前Activity(堆栈中最后一个压入的) /
public Activity currentActivity(){
Activity activity=activityStack.lastElement();
return activity;
} / 结束当前Activity(堆栈中最后一个压入的) */
public void finishActivity(){
Activity activity=activityStack.lastElement();
if(activity!=null)
{
activity.finish();
activity=null;
}
} / 结束指定的Activity /
public void finishActivity(Activity activity){
if(activity!=null)
{
activityStack.remove(activity);
activity.finish();
activity=null;
}
} /* 结束指定类名的Activity /
public void finishActivity(Class<?> cls){
for (Activity activity : activityStack)
{
if(activity.getClass().equals(cls) )
{
finishActivity(activity);
}
}
} / 结
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享
束所有Activity */
public void finishAllActivity(){
for (int i = 0, size = activityStack.size(); i < size; i++)
{
if (null != activityStack.get(i))
{
activityStack.get(i).finish();
}
}
activityStack.clear();
} / 退出应用程序 /
public void AppExit(Context context){
try
{
finishAllActivity();
ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); activityMgr.restartPackage(context.getPackageName());
System.exit(0);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/*封装一个BaseActivity类统一管理,给各个Activity继承/
/ Activity基类 @author Donkor /
public abstract class BaseActivity extends AppCompatActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//将Activity实例添加到AppManager的堆栈
AppManager.getAppManager().addActivity(this);
}
@Override protected void onDestroy()
{
super.onDestroy();
//将Activity实例从AppManager的堆栈中移除
AppManager.getAppManager().finishActivity(this);
}
}
/需要主页面或者主类(mainactivity)处理Dialog的显示问题,通过管理APP(AppManager)获取到当前栈顶的Activity,用于构造Dialog就行了/
Dialog myDialog = new Dialog(AppManager.getAppManager().currentActivity(), R.style.dialog_style);
3.单选Dialog
int yourChoice;
private void showSingleChoiceDialog(){
final String[] items = { "我是1","我是2","我是3","我是4", };
yourChoice = -1;
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
singleChoiceDialog.setTitle("我是一个单选Dialog");
singleChoiceDialog.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
yourChoice = which;
}
});
singleChoiceDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (yourChoice != -1) {
Toast.makeText(MainActivity.this,
"你选择了" + items[yourChoice],
Toast.LENGTH_SHORT).show();
}
}
});
singleChoiceDialog.show();
}
4.多选Dialog
ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
最后
愿你有一天,真爱自己,善待自己。
本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录
</div>
<div id="asideoffset"></div>
|