AlertDialog使用小结

使用Builder创建对话框的使用

(1)       创建AlertDialog.Builer对象

(2)       通过builder构建器对象对对话框设置标题,设置图标,设置内容等。

(3)       调用setPositionbutton()、setNegativeButton、setneutralButton()添加按钮

(4)       调用create()构建对话框,返回对话框对象,可以拿到对话框

(5)       调用show()展示对话框

构建器构建对话框是的方法

Setmessage();//设置对话框展示的信息,只能设置一项不能与setItems、setSingleChoiceItems()、setMultiChoiceItems()、setAdapter()、setAdapter()、setView()

setItems();//设置对话框内容为简单列表项

setSingleChoiceItems():设置对话框内容为单选列表项

setMultiChoiceItems():设置对话框内容为多选列表项

setAdapter():设置对话框内容为自定义列表项

setView():设置对话框内容为自定义View

setItems(String[] item,DialogInterface.OnClickListener)//设置对话框上的按钮个数和监听

setPositionbutton()

setNegativeButton()

setneutralButton()

setTitle()

setIcon()


builder.setCancelable(false);//设置点击外部不消失,true点击外部可以消失,默认是可以消失的

Alertdialog类的对象的setCanceledOnTouchOutside(false)方法同样可以实现

 


使用例子:http://pan.baidu.com/s/1miayrt2点击打开链接         

http://pan.baidu.com/s/1nu9XQTb点击打开链接

 

 

 

下面有一些项目中经常使用的对话框的使用

setItems(String[] item,DialogInterface.OnClickListener)//设置对话框上的按钮个数和监听    使用:

AlertDialog.Builder builder = new AlertDialog.Builder(
PdActivity.this);
builder.setTitle("商品操作列表");
// builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setItems(new String[] { "删除", "全部删除" },
new android.content.DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
case 0:
Toast.makeText(this, "删除", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(this, "全部删除", Toast.LENGTH_LONG).show();
break;
}
}
});
builder.setNegativeButton("取消", null);
builder.show();

设置对话框的占屏幕的大小

WindowManager m = getWindowManager();   

Display d = m.getDefaultDisplay();  //为获取屏幕宽、高    

android.view.WindowManager.LayoutParams p =dialog.getWindow().getAttributes();  //获取对话框当前的参数值    

p.height = (int)(d.getHeight()/6*4);   //高度设置为屏幕的4/6  

p.width = (int)(d.getWidth() /5*4);    //宽度设置为屏幕的4/5

dialog.getWindow().setAttributes(p);     //设置生效 

/**

          * @param flag

          * 利用java 的反射机制对当前对话框是否可以被关闭做控制

          */

         publicvoid isDialogCanBeClose(DialogInterface dialog,boolean flag) {

                   if(flag == true) {

                            //关闭对话框

                            try{

//java.lang.reflect.Field;

                                     Fieldfield = dialog.getClass().getSuperclass()

                                                        .getDeclaredField("mShowing");

                                     field.setAccessible(true);

                                     field.set(dialog,true); //dialog消失

                            }catch (Exception e) {

                                     e.printStackTrace();

                            }

                   }else {

                            //不关闭对话框

                            try{

                                     Fieldfield = dialog.getClass().getSuperclass()

                                                        .getDeclaredField("mShowing");

                                     field.setAccessible(true);

                                     field.set(dialog,false); //dialog不消失

                            }catch (Exception e) {

                                     e.printStackTrace();

                            }

                   }

         }

 

 

/**

          * 显示信息

          * @param title

          * @param content

          */

         public voidshowDialogChose(String title,String content,DialogInterface.OnClickListenerlistener){

                   AlertDialog.Builderbuilder=new AlertDialog.Builder(this);

                   builder.setTitle(title);

                   builder.setIcon(R.drawable.zydialog);

                   builder.setMessage(content);

                   builder.setNegativeButton("取消", listener);

                   builder.setPositiveButton("确定", listener);

                   builder.create().show();

         }

 

/**

          * @param title

          * @param content

          *  点击确定返回上一个页面

          */

         publicvoid ShowDialogFinish(String title, String content) {

 

                   AlertDialog.Builderbuilder = new AlertDialog.Builder(this);

                   builder.setTitle(title);

                   builder.setIcon(R.drawable.zydialog);

                   builder.setMessage(content);

                   builder.setNegativeButton("确定", new OnClickListener() {

                            publicvoid onClick(DialogInterface dialog,int whichButton) {

                                     BaseActivity.this.finish();

                                     BaseActivity.this.overridePendingTransition(

                                                        R.anim.in_from_left,R.anim.out_to_right);

                                     //context.finish();

                            }

                   });

 

                   builder.create().show();

         }

 

/**

          * 确定弹出框

          *

          * @param 标题

          * @param 内容

          */

         publicvoid ShowDialog(String title, String content) {

                   AlertDialog.Builderbuilder = new AlertDialog.Builder(this);

                   builder.setTitle(title);

                   builder.setIcon(R.drawable.zydialog);

                   builder.setMessage(content);

                   builder.setNegativeButton("确定", new OnClickListener() {

                            publicvoid onClick(DialogInterface dialog,int whichButton) {

                                     //do nothing

                            }

                   });

                   builder.create().show();

         }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值