zoukankan      html  css  js  c++  java
  • 对话框AlertDialog的dismiss和cancel区别

    先上cancel方法源码和注解

      /**
         * Cancel the dialog.  This is essentially the same as calling {@link #dismiss()}, but it will
         * also call your {@link DialogInterface.OnCancelListener} (if registered).
         */
        public void cancel() {
            if (!mCanceled && mCancelMessage != null) {
                mCanceled = true;
                // Obtain a new message so this dialog can be re-used
                Message.obtain(mCancelMessage).sendToTarget();
            }
            dismiss();
        }

    注解的大意:cancel对话框,这个在本质上基本和disimss一样。但是cancel会执行DialogInterface.OnCancelListener监听,前提是有注册过。

    public void openDialog(View v){
            AlertDialog dialog ;
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("标题");
            builder.setMessage("dismiss和cancel的区别");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            
            builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            
            dialog = builder.create();
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    Toast.makeText(MainActivity.this, "setOnCancelListener", Toast.LENGTH_SHORT).show();
                }
            });
            dialog.show();
        }

    点击确定按钮后会执行DialogInterface.OnCancelListener();弹出Toast且关闭对话框。

  • 相关阅读:
    P1265 公路修建 最小生成树
    P1991 无线通讯网 最小生成树
    Stock Chase 拓扑
    Rank of Tetris 拓扑排序+并查集
    P1169 [ZJOI2007]棋盘制作 DP悬线法
    P4147 玉蟾宫 二维DP 悬线法
    P1341 无序字母对 欧拉回路
    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs
    BSTestRunner插件的用法.py
    Selenium
  • 原文地址:https://www.cnblogs.com/datian/p/3895002.html
Copyright © 2011-2022 走看看