自定义自己的AlertDialog

android提供的弹出框很单调,往往在开发中需要定义自己的AlertDialog来适应开发的需要

android用户帮助里面的方法是这样:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("Are you sure you want to exit?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
                MyActivity.this.finish(); 
           } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
                dialog.cancel(); 
           } 
       }); 
AlertDialog alert = builder.create();

 

效果如图

1 实现单选框的弹出框

  

builder = new AlertDialog.Builder(MainActivity
    .this);
  builder.setTitle("选择哪个?")
  .setSingleChoiceItems(item,gameMethodValuesIndex,new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
    gameMethodValuesIndex = which;
    Toast.makeText(getApplicationContext(), ""+item[which], Toast.LENGTH_SHORT).show();
   }
  })
  .setPositiveButton("确定", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
   }
  })
  .setNegativeButton("取消", null);
  AlertDialog dialog = builder.create();
  dialog.show();

   item 是一个数组,自己定义一个就行了,编辑器不太会用,暂不上传了

 效果如图:

(本地图片上传不了。。。擦,还是我没有找到方法?有知道的童鞋告诉一下,俺只找到了上传网络图片的方法)

  要实现上面的效果还可以这样,自定义配置文件

  

 LayoutInflater inflater = null;
        inflater = (LayoutInflater) getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.main, null);
		builder = new AlertDialog.Builder(MainActivity
				.this);
		builder.setTitle("选择哪个?")
		
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		})
		.setNegativeButton("取消", null)
		.setView(view)
		;
		AlertDialog dialog = builder.create();
		dialog.show();

 

掌握了这个用法基本上自定义alertdialog就没什么了,可以应对大部分的开发

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值