Pop up a top alert Dialog like TYPE_SYSTEM_ALERT

当Dialog有编辑框时如果选择会弹菜单窗口就不要用

package com.android.autologin;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.widget.EditText;

/**
 *     This is the dialog Activity used as an AlertDialog for the user to interact.
 * Requires android:launchMode="singleInstance" in your AndroidManifest to work
 * properly.
 * <p/>
 *     当Dialog有编辑框时如果选择会弹菜单窗口就不要用 AlertDialog(Application)
 * 再添加WindowManager.LayoutParams. TYPE_SYSTEM_ALERT,
 * <pre>
 *         Context applicationContext = mainActivity.getApplicationContext();
 *        AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(applicationContext);
 *        EditText et = new EditText(mainActivity);
 *        dlgBuilder.setTitle(mainActivity.getTitle());
 *        dlgBuilder.setPositiveButton(android.R.string.ok, null);
 *        dlgBuilder.setNegativeButton(android.R.string.cancel, null);
 *        dlgBuilder.setIcon(android.R.drawable.ic_dialog_alert);
 *        dlgBuilder.setView(et);
 *        AlertDialog dialog = dlgBuilder.create();
 *        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
 *        dialog.show();
 * </pre>
 * 防止报错
 * attrs.token = null,
 * BadTokenException("Unable to add window -- token " + attrs.token + " is not for an application")
 *
 * @usage
 *
 * <i>.java</i>
 *
 * <code><pre>
 *         Intent dialogIntent = new Intent(mContext, AlertActivity.class);
 *         dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 *         mContext.startActivity(dialogIntent);
 * </pre></code>
 *
 * <i>AndroidManifest.xml</i>
 *
 * <pre>
 *         < activity
 *             android:name=".AlertActivity"
 *             android:excludeFromRecents="true"
 *             android:finishOnTaskLaunch="true"
 *             android:launchMode="singleInstance"
 *             android:theme="@android:style/Theme.Dialog" >
 *         < /activity>
 * </pre>
 *
 * @author Sansan
 **/
public class AlertActivity extends Activity implements DialogInterface.OnClickListener, OnDismissListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // setContentView(R.layout.alertactivity);

        // Get root view from current activity
        // @see http://stackoverflow.com/questions/4486034/get-root-view-from-current-activity

        // //rootview
        //// check layouts in hierarchyviewer.

        // findViewById(android.R.id.content) is giving me the root view
        // findViewById(android.R.id.content).getRootView()
        //getWindow().getDecorView().findViewById(android.R.id.content)

        // //-------------------------------------------------------------------------------------------
        // //This is what I use to get the root view as found in the XML file assigned with setContentView
        // //This answer gave the view without the status bar - which is what I wanted. I was looking for
        // //the pixel width + height of the visible part of the activity. This one works, thanks!
        //
        //This excludes ActionBar!
        //final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        // //-------------------------------------------------------------------------------------------

        // //-------------------------------------------------------------------------------------------
        // //I tested this in android 4.0.3, only:
        //
        // getWindow().getDecorView().getRootView();
        //
        // //give the same view what we get from
        //
        // anyview.getRootView();  == [email protected]#########
        //
        // //and
        //
        // getWindow().getDecorView().findViewById(android.R.id.content)

        // //giving child of its == [email protected]#######
        // //Please confirm.
        // //-------------------------------------------------------------------------------------------

        //getWindow().getDecorView().setVisibility(View.GONE);

        //requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏

        // //Error, if select any text to pop up a system menu view, such as
        // paste in
        // // the Dialog.
        // //@see android/view/ViewRootImpl.java
        // //case WindowManagerGlobal.ADD_NOT_APP_TOKEN: throw new
        // //WindowManager.BadTokenException("Unable to add window -- token " +
        // //attrs.token + " is not for an application");
        // Context applicationContext = this.getApplicationContext();
        // AlertDialog.Builder dlgBuilder = new
        // AlertDialog.Builder(applicationContext);

        AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(this);
        EditText et = new EditText(this);

        dlgBuilder.setTitle(this.getTitle());
        dlgBuilder.setPositiveButton(android.R.string.ok, AlertActivity.this);
        dlgBuilder.setNegativeButton(android.R.string.cancel, AlertActivity.this);
        dlgBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        dlgBuilder.setView(et);
        AlertDialog dialog = dlgBuilder.create();
        // dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnDismissListener(this);
        dialog.show();
    }

    /**
     * meanwhile, finish the AlertActivity.
     */
    @Override
    public void onDismiss(DialogInterface dialog) {
        finish();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
}
时间: 2024-10-12 14:57:42

Pop up a top alert Dialog like TYPE_SYSTEM_ALERT的相关文章

【转】 Android常用实例—Alert Dialog的使用

Android常用实例-Alert Dialog的使用 AlertDialog的使用很普遍,在应用中当你想要用户做出"是"或"否"或者其它各式各样的选择时,为了保持在同样的Activity和不改变用户屏幕,就可以使用AlertDialog. 代码地址 https://github.com/JueYingCoder/AndroidUsefulExample_AlertDialog 这篇文章主要讲解如何实现各种AlertDialog,文章比较长,如果能认真读完,Aler

Android常用实例—Alert Dialog的使用

Android常用实例-Alert Dialog的使用 AlertDialog的使用很普遍,在应用中当你想要用户做出"是"或"否"或者其它各式各样的选择时,为了保持在同样的Activity和不改变用户屏幕,就可以使用AlertDialog. 这篇文章主要讲解如何实现各种AlertDialog,文章比较长,如果能认真读完,AlertDialog的各种用法应该就能掌握了,下面是我们今天要实现的最终效果: 乍一看,在应用中我们见过很多千奇百怪的对话框,但仔细分析,它还是有

Android Alert Dialog解决点击按钮对话框不关闭的问题

Android alertDialog中左中右三个按钮,点击按钮的时候,即使没调用dismiss方法,系统也会默认的去调用,而把这个对话框被关闭掉,但是我们的一些实际需求,就需要保留这个对话框不动,例如输入校验码,密码之类的校验问题,如果用户输入错误,而关闭后弹出对话框,就很别扭了.这里有个方案可以解决这个问题,使得即使按钮事件发生了,也不会关闭对话框.核心思想是欺骗一下系统,在点击事件里,修改该对话框的显示状态为关闭,则系统就不会去关闭该对话框了.具体实现代码如下: /** @author s

好看的dialog,sweet Alert Dialog 导入Android Studio

系统自带的dialog实在是丑到无法忍受.所以找到了一款比较好的第三方dialog. github 地址如下:https://github.com/pedant/sweet-alert-dialog 老规矩,还是先看效果图! 下面来介绍导入Android studio的方法 首先将github上的项目clone到本地.然后将项目放到你的app根目录下 一.修改settings.gradle  添加include, 修改后如下: include 'sweet-alert-dialog:librar

Install MongoDB on Windows

Overview Use this tutorial to install MongoDB on a Windows systems. PLATFORM SUPPORT Starting in version 2.2, MongoDB does not support Windows XP. Please use a more recent version of Windows to use more recent releases of MongoDB. IMPORTANT If you ar

Install MongoDB on Windows (Windows下安装MongoDB)

Install MongoDB on Windows Overview Use this tutorial to install MongoDB on a Windows systems. PLATFORM SUPPORT Starting in version 2.2, MongoDB does not support Windows XP. Please use a more recent version of Windows to use more recent releases of M

MongoDB随手记2——安装db

以下为学习笔记: 1. 去MongoDB download center下载适合电脑操作系统的安装文件: https://www.mongodb.com/download-center#enterprise 2. 找到.msi文件双击运行按照步骤进行安装即可. Set up the MongoDB environment. MongoDB requires a data directory to store all data. MongoDB's default data directory p

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈

小结: 1. 常数时间内检索到最小元素 最小栈 - 力扣(LeetCode)https://leetcode-cn.com/problems/min-stack/ 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. getMin() -- 检索栈中的最小元素. 示例: MinStack minStack = new MinStack(); minSt

jQuery Dialog弹出层对话框插件

Dialog.js的相关注释已经添加,可以按照注释,进行相关样式的修改,适用于自定义的各个系统! dialog.js 1 /** 2 * jQuery的Dialog插件. 3 * 4 * @param object content 5 * @param object options 选项. 6 * @return 7 */ 8 function Dialog(content, options) 9 { 10 var defaults = { // 默认值. 11 title:'标题', // 标