首页 > 代码库 > Pop up a top alert Dialog like TYPE_SYSTEM_ALERT

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();  == com.android.internal.policy.impl.PhoneWindow$DecorView@#########        //        // //and        //        // getWindow().getDecorView().findViewById(android.R.id.content)                // //giving child of its == android.widget.FrameLayout@#######        // //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) {    }}

 

Pop up a top alert Dialog like TYPE_SYSTEM_ALERT