Android 使用AlertDialog.Builder构造对话框dialog全过程

来源:互联网 发布:济南市行知小学 编辑:程序博客网 时间:2024/05/22 06:55

工作内容:

1.AlertDialog.Builder的使用(构造一个对话框)(系统自带的对话框样式)

2.自定义dialog对话框工具类,适应所有的需要

学习分享:

一、构造对话框全解:【拨号对话框——点击对话框中”确定按钮“实现拨号】

1.前提:在AndroidManifest中设置“拨号”权限:

 需在<application>上方创建,下面是权限设置代码段

<uses-permission android:name="android.permission.CALL_PHONE"/>
<application    android:allowBackup="true"    android:icon="@mipmap/ic_launcher"

2.显示dialog方法,如下【phoneCall对象包含2个属性:1.name,2.phoneNumber

public voidshowDialog(){
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);//构造一个dialog
    
dialog.setTitle("拨号提示")     //设置标题
            
.setMessage("是否拨打号码:"+phoneCall.getPhoneNumeber())  //设置内容 phoneCall.getPhoneNumeber()是一个电话号码
            //给拨号按钮添加点击事件
            
.setNegativeButton("拨号",newDialogInterface.OnClickListener() {
                @Override
                public voidonClick(DialogInterface dialog,intwhich) {
                    //生成一个拨号intent
                    
Intent intent =newIntent(Intent.ACTION_CALL);
                    //设置intent的拨号数据(Uri.parse("tel:"+phoneCall.getPhoneNumeber())构造了一个Uri对象)
                    
intent.setData(Uri.parse("tel:"+phoneCall.getPhoneNumeber()));
                    startActivity(intent);
                }
            })
            //setNegativeButton取消按钮(显示在setNegativeButton按钮的右边)
            
.setPositiveButton("取消",null)
            //设置是否取消
            
.setCancelable(true);
    //让dialog显示
    
dialog.show();
}

二、自定义对话框工具类:调用系统的对话框样式

public class DialogTools {    /**     * 使用系统的对话框样式     * @param context   上下文(显示在哪个界面)     * @param title     对话框标题     * @param message   对话框内容     * @param intent    点击确定按钮后跳转的页面     */    public static void open(final Context context, String title, String message, final Intent intent){        AlertDialog.Builder dialog = new AlertDialog.Builder(context);        dialog.setTitle(title)                .setMessage(message)                .setPositiveButton("取消",null)                .setNegativeButton("确定", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        context.startActivity(intent);                    }                }).setCancelable(true)                .create();        dialog.show();    }}

调用方法:

//在一个Activity中调用
//生成一个拨号intentIntent intent = new Intent(Intent.ACTION_CALL);//设置intent的拨号数据(Uri.parse("tel:"+phoneCall.getPhoneNumeber())构造了一个Uri对象)intent.setData(Uri.parse("tel:"+phoneCall.getPhoneNumeber()));DialogTools.open(this,"拨号提示","是否拨打号码"+phoneCall.getPhoneNumeber(),intent);

0 0
原创粉丝点击