Android 常用dialog提示对话框

在android应用程序中,经常需要用到dialog对话框让用户知道现在所在进行的操作(比如耗时的操作),或者提示某些信息和状态等,算是比较常用的一个知识点;

1、简单对话框

    protected void dialog1(AlertDialog.Builder builder) {
        builder.setTitle("标题");
        builder.setPositiveButton("确定", null);
        builder.setIcon(R.drawable.ic_launcher);
        builder.setMessage("简单消息框");
        builder.show();
    }

2、带自定义内容对话框

    protected void study2(AlertDialog.Builder builder) {
        builder.setTitle("标题");
        builder.setView(new EditText(this));
        builder.setPositiveButton("确定", null);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setMessage("简单消息框");
        builder.show();
    }

3、带单选按钮对话框

    protected void study3(AlertDialog.Builder builder) {
        builder.setTitle("请选择")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setSingleChoiceItems(
                        new String[] { "item1", "item2", "item3", "item4" }, 0,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                Log.e("选择", "" + which);
                                dialog.dismiss();
                            }
                        }).setNegativeButton("取消", null).show();
    }

在选择了某一项之后,onClick回调事件会把选择的项的索引返回给用户;

4、带多选组合框对话框

    protected void study4(AlertDialog.Builder builder) {
        builder.setTitle("请选择")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setMultiChoiceItems(
                        new String[] { "item1", "item2", "item3", "item4" },
                        new boolean[] { true, true, false, true },
                        new OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which, boolean isChecked) {
                                ListView lv = ((AlertDialog) dialog).getListView();
                                Log.e("项" + which, "选择" + lv.getCheckedItemPositions().get(which));
                            }
                        }).setNegativeButton("取消", null).show();
    }

在多选对话框中,选择了某一项并不会导致对话框隐藏,同样android也通过回调接口返回用户所选择的项;

5、进度条对话框

    public void study5() {
        ProgressDialog dialog = new ProgressDialog(MainActivity.this);
        dialog.setCancelable(true);
        dialog.setMessage("加载中...");
        // dialog.setTitle("进度条框窗口");
        // dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // dialog.setMax(100);
        dialog.show();
    }

 

6、除了上述的添加用户界面友好提示的方法,还可以通过WindowManager添加一个View到界面上向用户反馈信息,此种方法也更灵活

    protected void study6() {
        ViewGroup vg = (ViewGroup) getLayoutInflater().inflate(R.layout.dia,null);
        pb = (ProgressBar) vg.findViewById(R.id.pb);
        vg.removeAllViews();
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(160,
                160, WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        windowManager.addView(pb, lp);
    }

重要的步骤即是从lp开始,设置LayoutParams参数,然后添加到窗口;

7.使用Window将view添加到window上

                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                AlertDialog dialog = builder.show();
                Window window = dialog.getWindow();
                window.setContentView(R.layout.dialoglayout);

8.使用style来设置样式

    <!-- 自定义Dialog -->
    <style name="MyDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

//

                Dialog dialog1 = new Dialog(getActivity(), R.style.MyDialog);
                dialog1.setContentView(R.layout.dialoglayout);
                dialog1.show();

 

 

 

posted @ 2013-10-07 16:45  Livingstone  阅读(5273)  评论(0编辑  收藏  举报