AlertDialog对话框的几种样式

          使用手机的时候我们都注意到,经常会在界面上弹出一些对话框,比如简单的信息提示、询问用户是否退出或者让用户选择亦或者是输入一些简单的信息。这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,简单归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助。

1.简单的信息提示框

此处setPositiveButton("确定", null)传入的null值是不实现“确认”的点击事件,具体怎么实现下面会有。

           AlertDialog对话框的几种样式_第1张图片   

AlertDialog.Builder dialog1=new AlertDialog.Builder(DialogActivity.this);
dialog1.setTitle("标题").setMessage("这是一个简单的消息框!").setPositiveButton("确定", null).show();
			

2. 确认对话框

AlertDialog对话框的几种样式_第2张图片

通常在执行删除或者其他一些需要用户进行确认的时候出现。代码如下:

 

AlertDialog.Builder dialog2=new AlertDialog.Builder(DialogActivity.this);
dialog2.setTitle("提示").setMessage("确认删除?").setPositiveButton("确定", null).setNegativeButton("取消", null).show();
			

3.有多个按钮的对话框

AlertDialog对话框的几种样式_第3张图片

在特定情况下,除了需要有“确定”和“取消”两个按钮外还会有一个查看详情按钮。代码如下:

AlertDialog.Builder dialog3=new AlertDialog.Builder(DialogActivity.this);
dialog3.setTitle("警告").setMessage("确认删除?").setPositiveButton("确定", null).setNegativeButton("取消", null).setNeutralButton("查看详情", null).show();
			

4.多选项单选对话框

AlertDialog对话框的几种样式_第4张图片         

  AlertDialog.Builder dialog5=new AlertDialog.Builder(DialogActivity.this);
                        final String[] arrayString=new String[]{"张三","李四","王五","赵六","陈七","梁八"};
                        dialog5.setTitle("谁是程序员?");
                        dialog5.setIcon(R.drawable.ic_launcher).setItems(arrayString, new DialogInterface.OnClickListener() {	
			@Override
			public void onClick(DialogInterface dialog, int which) {
					// which参数是当前点击的索引
                                        Toast.makeText(DialogActivity.this, arrayString[which]+"是程序员", Toast.LENGTH_SHORT).show();
				}
			}).setNegativeButton("取消", null).show();
			

5.多选框对话框

AlertDialog对话框的几种样式_第5张图片

  AlertDialog.Builder dialog6=new AlertDialog.Builder(DialogActivity.this);
			final String[] arrayString1=new String[]{"张三","李四","王五","赵六","陈七","梁八"};
			
			dialog6.setTitle("谁是程序员?");
			dialog6.setIcon(R.drawable.ic_launcher).setMultiChoiceItems(arrayString1, null, null).setPositiveButton("确定", null).setNegativeButton("取消", null).show();
			

6.自定义对话框

AlertDialog对话框的几种样式_第6张图片

有时候我们在登录的时候会弹出一个对话框输入账号密码。此时就用到了自定义的对话框。

首先创建一个自定义的登录界面:



    

        

        

        
        
    
	
	     

        

        
	

然后在弹出对话框的事件中对这个布局进行引用

                        LayoutInflater layoutInflater=LayoutInflater.from(DialogActivity.this);
			View loginView=layoutInflater.inflate(R.layout.login, null);
			AlertDialog.Builder dialog7=new AlertDialog.Builder(DialogActivity.this);
			dialog7.setTitle("登录").setView(loginView).setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					//在这里对“确定”事件进行处理
				}
			}).setNegativeButton("取消", null).show();
其中setView()是用来加载View的,可以是自己定义的一个布局文件,也可以自己传入一个文本编辑框,当然,你可以传入任何的视图对象,比如图片框,WebView等。。只有想不到~

例如:

传入文本框

AlertDialog对话框的几种样式_第7张图片

传入图片

AlertDialog对话框的几种样式_第8张图片



  最后总结一下,android平台为我们开发提供了极大的便利,AlertDialog.Builder能做的不止这些,这里给大家展示的只是冰山一角,我们可以尽情的发挥想象,创造我们自己的对话框。

你可能感兴趣的:(Android)