`
v5browser
  • 浏览: 1139037 次
社区版块
存档分类
最新评论

安卓ApiDemos学习 app/Dialog

 
阅读更多

类AlertDialogSamples展示各种形态的dilalog

在各个按钮的click函数中,调用showDialog。然后重写activity的onCreateDialog方法来弹出dialog

传入参数为dialog id

下面分别分析

1、OK Cancel dialog with a message

yes/no类型的dialog。

setPositiveButton方法第一个参数是按钮文字id,爹热参数是按钮按下的监听。这里处理ok事件

类似的,setNegativeButton方法处理cancel事件

new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_two_buttons_title)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Cancel so do some stuff */
                    }
                })
                .create();
2、OK Cancel dialog with a long message

除了设置dialog的title外,还设置了Message属性

setMessage(R.string.alert_dialog_two_buttons2_msg)

经过测试发现,dialog弹出后,可以修改title,却不能修改message

设置了setNeutralButton方法,这样,画面上有了三个按钮:ok,something,cancel

3、List dialog

设置了dialog的item属性。

new AlertDialog.Builder(AlertDialogSamples.this)
                .setTitle(R.string.select_dialog)
                .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        /* User clicked so do some stuff */
                        String[] items = getResources().getStringArray(R.array.select_dialog_items);
                        new AlertDialog.Builder(AlertDialogSamples.this)
                                .setMessage("You selected: " + which + " , " + items[which])
                                .show();
                    }
                })

本例子是从资源文件中读取item情报

    <string-array name="select_dialog_items">
        <item>Command one</item>
        <item>Command two</item>
        <item>Command three</item>
        <item>Command four</item>
    </string-array>
点击某个item后,根据传入的which参数,判断点击的是第几个item,来作出动作。

4、Progress dialog

Progress dialog有2种形态,STYLE_HORIZONTAL是进度条式,STYLE_SPINNER是一个不停旋转的圈

可以设置setMax来表示进度条的最大数值,本例是100

如果不能确定执行的具体时间,可以设置setIndeterminate,让进度条变成时间不定的状态,对于STYLE_HORIZONTAL,就是一个进度块左右滑动。

mProgressDialog = new ProgressDialog(AlertDialogSamples.this);
mProgressDialog.setIcon(R.drawable.alert_dialog_icon);
mProgressDialog.setTitle(R.string.select_dialog);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(MAX_PROGRESS);

弹出dialog后,利用来控制进度条Handler,每隔100ms更新一次,让进度条的进度增加1

        mProgressHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (mProgress >= MAX_PROGRESS) {
                    mProgressDialog.dismiss();
                } else {
                    mProgress++;
                    mProgressDialog.incrementProgressBy(1);
                    mProgressHandler.sendEmptyMessageDelayed(0, 100);
                }
            }
        };

5、Single choice list
和上面的setItems类似,不过这里设置setSingleChoiceItems

    <string-array name="select_dialog_items2">
        <item>Map</item>
        <item>Satellite</item>
        <item>Traffic</item>
        <item>Street view</item>
    </string-array>
和list类型一样,监听函数的whichButton参数表示选择项目的index。下面可以验证这个结论

setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked on a radio button do some stuff */
                        String[] items = getResources().getStringArray(R.array.select_dialog_items2);
                        new AlertDialog.Builder(AlertDialogSamples.this)
                        .setMessage("You selected: " + whichButton + " , " + items[whichButton])
                        .show();
                    }
                })

6、Repeat alarm

使用函数setMultiChoiceItems设置checkbox类型的dialog。

第二个参数设置默认选择值

new boolean[]{false, true, false, true, false, false, false}

第三个参数表示被选中项目的index,第4个参数表示被选中的状态

setMultiChoiceItems(R.array.select_dialog_items3,
                        new boolean[]{false, true, false, true, false, false, false},
                        new DialogInterface.OnMultiChoiceClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton,
                                    boolean isChecked) {

                                /* User clicked on a check box do some stuff */
                            }
                        })

7、Send Call to VoiceMail

待补充


8、Text Entry dialog

利用自定义的layout

首先从资源文件实例化layout

LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
初始化dialog时,setView(textEntryView)

在按钮处理函数中,可以利用 textEntryView.findViewById得到自定义layout上的控件

这样就可以得到画面上的值


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics