搜索
您的当前位置:首页正文

android常见的alertdialog与实际问题解决方案

来源:筏尚旅游网

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

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

/** @author sanji* 假设对话框已经关闭,欺骗系统,以保持输入窗口*/

private void setDialogNotShow(){

   if(mDialogInterface!=null){

      try {

         Field field = mDialogInterface.getClass() .getSuperclass().getDeclaredField( "mShowing" );

         field.setAccessible( true ); // 将mShowing变量设为false,表示对话框已关闭

         field.set(mDialogInterface, false );

      } catch (Exception e){

         e.printStackTrace();

      }

   }

}

mDialogInterface是点击对话框按钮事件传进来的DialogInterface dialog参数,如果再需要销毁该对话框的话,则:

private void closeDialogOfPINorPUK(){

    if(mDialogInterface!=null){

       try {

          Field field = mDialogInterface.getClass() .getSuperclass().getDeclaredField( "mShowing" );

          field.setAccessible( true );

          field.set(mDialogInterface, true );

          mDialogInterface.dismiss();

       }catch (Exception e){

          e.printStackTrace();

       }

    }

}

1.问题原由和处理方案:开机SIM卡读取太慢,造成PIN码输入弹出界面太慢,为了在弹出之前避免用户操作界面,采用了弹出全屏dialog提示用户的解决方案
解决方案中关于自定义dialog的部分代码:
View layout = inflater.inflate(R.layout.checking_simcard_dialog,null);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext,R.style.DialogFullscreen);
builder.setView(layout);
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
dialog.setCancelable(false);
dialog.show();
代码分析:
AlertDialog.Builder builder = new AlertDialog.Builder(mContext,R.style.DialogFullscreen);

通过AlertDialog.Builder方式创建一个自定义全屏的dialog,其中样式DialogFullscreen代码为:
<style name=”DialogFullscreen”>
<item name=”android:windowBackground”>@color/black</item>
<item name=”android:windowFullscreen”>true</item>
</style>

builder.setView(layout);  自定义布局的dialog
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);   dialog的级别层为TYPE_KEYGUARD_DIALOG
dialog.setCancelable(false);  不让点击其它dialog其它地方取消当前dialog

延伸知识:
1.dialog的显示层:从解决方案中的WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG,得 知该dialog显示的层为与锁屏同样的锁屏层,查看对应的WindowManager.LayoutParams中会有很多不同的层,例 如:TYPE_STATUS_BAR状态栏层;TYPE_TOAST  toast所在的弹出层;TYPE_PRIORITY_PHONE 来电界面显示层;TYPE_SYSTEM_DIALOG 系统级dialog层;TYPE_SYSTEM_ERROR系统级错误层;TYPE_WALLPAPER 壁纸层 等等。可以根据不同的情况选择不同的层。
2.关于android alertdialog的不同形式:收集了相关资料,了解了alertdialog的不同形式以及详细见下面回复:

在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助。
1.该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式。

03
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
04
android:layout_height=”wrap_content” android:layout_width=”wrap_content”
05
android:background=”#ffffffff” android

因篇幅问题不能全部显示,请点此查看更多更全内容

Top