Android 对话框(Dialog)大全 建立你自己的对话框

Android 对话框(Dialog)大全 建立你自己的对话框

 

转载自:http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html

 

 

下面是几种对话框的效果:

 

 


(注意:按1下[Back]按键同样会出现该对话框)

图一

 


 

图二

 

 


图三

 

 


图四

 


图五

 


图六

 


图七

 


图八

 

 


以上对话框效果对应的实现代码:

 

图一效果具体代码

 

main.xml布局配置

 

Xml代码   收藏代码
  1. <Button    
  2.     android:id="@+id/idButtonDialog1"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"   
  5.     android:text="Dialog1"  
  6.     android:onClick="clickButtonDialog1"  
  7.     />  
 

 

Activity代码

 

Java代码   收藏代码
  1. /** 
  2.      * 重载当前Activity的[键盘按下]事件处理方法. 
  3.      */  
  4.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  5.         //KeyEvent.KEYCODE_BACK:键盘上的[back]按键  
  6.         //捕获按键:按下[back]键并且没有重复  
  7.         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
  8.             showDialog1();  
  9.         }  
  10.         return false;  
  11.     }  
  12.       
  13.   
  14.     public void clickButtonDialog1(View v){  
  15.         showDialog1();  
  16.     }  
  17.       
  18.     public void showDialog1(){  
  19.         AlertDialog.Builder builder = new Builder(this)  
  20.         .setMessage("确认退出吗?")//设置对话框内容  
  21.         .setTitle("提示")//设置对话框标题  
  22.         .setPositiveButton("确定"new OnClickListener() {//设置对话框[肯定]按钮  
  23.             @Override  
  24.             public void onClick(DialogInterface dialog, int which) {  
  25.                 dialog.dismiss();//关闭对话框  
  26.                 finish();//结束当前Activity  
  27.             }  
  28.         })  
  29.         .setNegativeButton("取消"new OnClickListener() {//设置对话框[否定]按钮  
  30.             @Override  
  31.             public void onClick(DialogInterface dialog, int which) {  
  32.                 dialog.dismiss();//关闭对话框  
  33.             }  
  34.         });  
  35.         builder.create().show();//创建对话框并且显示该对话框  
  36.     }  
 

 

图二效果具体代码

 

main.xml布局配置

Xml代码   收藏代码
  1. <Button    
  2.     android:id="@+id/idButtonDialog2"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"   
  5.     android:text="Dialog2"  
  6.     android:onClick="clickButtonDialog2"  
  7.     />  
 

 

 

 

Activity代码

 

Java代码   收藏代码
  1. public void clickButtonDialog2(View v){  
  2.     showDialog2();  
  3.    }  
  4.      
  5. public void showDialog2(){  
  6.     final Context context = this;  
  7.     Dialog dialog = new AlertDialog.Builder(context)  
  8.     .setIcon(android.R.drawable.btn_star)//设置对话框图标  
  9.     .setTitle("喜好调查")//设置对话框标题  
  10.     .setMessage("你喜欢李连杰的电影吗?")//设置对话框内容  
  11.     .setPositiveButton("很喜欢"new OnClickListener(){//设置对话框[肯定]按钮  
  12.         @Override  
  13.         public void onClick(DialogInterface dialog, int which) {  
  14.             Toast.makeText(context, "我很喜欢他的电影。", Toast.LENGTH_LONG).show();  
  15.   
  16.         }  
  17.     })  
  18.     .setNegativeButton("不喜欢"new OnClickListener(){//设置对话框[否定]按钮  
  19.         @Override  
  20.         public void onClick(DialogInterface dialog, int which) {  
  21.             Toast.makeText(context, "我不喜欢他的电影。", Toast.LENGTH_LONG).show();  
  22.         }  
  23.     })  
  24.     .setNeutralButton("一般"new OnClickListener(){//设置对话框[中性]按钮  
  25.         @Override  
  26.         public void onClick(DialogInterface dialog, int which) {  
  27.             Toast.makeText(context, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG).show();  
  28.         }  
  29.     }).create();//创建对话框  
  30.       
  31.     dialog.show();//显示对话框  
  32. }  
 

 

图三效果具体代码

 

main.xml布局配置

Xml代码   收藏代码
  1. <Button    
  2.     android:id="@+id/idButtonDialog3"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"   
  5.     android:text="Dialog3"  
  6.     android:onClick="clickButtonDialog3"  
  7.     />  
 

 

 

Activity代码

Java代码   收藏代码
  1.        public void clickButtonDialog3(View v){  
  2.     showDialog3();  
  3.    }  
  4.      
  5. public void showDialog3(){  
  6.     final Context context = this;  
  7.     //定义1个文本输入框  
  8.     final EditText userName = new EditText(this);  
  9.     //创建对话框  
  10.     new AlertDialog.Builder(context)  
  11.     .setTitle("请输入")//设置对话框标题  
  12.     .setIcon(android.R.drawable.ic_dialog_info)//设置对话框图标  
  13.     .setView(userName)//为对话框添加要显示的组件  
  14.     .setPositiveButton("确定"new OnClickListener(){//设置对话框[肯定]按钮  
  15.         @Override  
  16.         public void onClick(DialogInterface dialog, int which) {  
  17.             Toast.makeText(context, "userName="+userName.getText().toString(), Toast.LENGTH_LONG).show();  
  18.         }  
  19.     })  
  20.     .setNegativeButton("取消"null)//设置对话框[否定]按钮  
  21.     .show();  
  22. }  
 

 

 

 

 

图四效果具体代码

 

main.xml布局配置

Xml代码   收藏代码
  1. <Button    
  2.     android:id="@+id/idButtonDialog4"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"   
  5.     android:text="Dialog4"  
  6.     android:onClick="clickButtonDialog4"  
  7.     />  
 

 

 

Activity代码

Java代码   收藏代码
  1. public void clickButtonDialog4(View v){  
  2.     showDialog4();  
  3.    }  
  4.      
  5. public void showDialog4(){  
  6.     final Context context = this;  
  7.     //定义单选框选项  
  8.     String[] singleChoiceItems = {"Item1","Item2"};  
  9.     int defaultSelectedIndex = 1;//单选框默认值:从0开始  
  10.     //创建对话框  
  11.     new AlertDialog.Builder(context)  
  12.     .setTitle("单选框")//设置对话框标题  
  13.     .setIcon(android.R.drawable.ic_dialog_info)//设置对话框图标  
  14.     .setSingleChoiceItems(singleChoiceItems, defaultSelectedIndex, new OnClickListener(){  
  15.         @Override  
  16.         public void onClick(DialogInterface dialog, int which) {  
  17.             Toast.makeText(context, "Item is "+which, Toast.LENGTH_LONG).show();  
  18.         }  
  19.     })  
  20.     .setPositiveButton("确定"null)//设置对话框[肯定]按钮  
  21.     .setNegativeButton("取消"null)//设置对话框[否定]按钮  
  22.     .show();  
  23. }  
 

图五效果具体代码

 

main.xml布局配置

Xml代码   收藏代码
  1. <Button    
  2.     android:id="@+id/idButtonDialog5"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"   
  5.     android:text="Dialog5"  
  6.     android:onClick="clickButtonDialog5"  
  7.     />  
 

 

 

Activity代码

Java代码   收藏代码
  1. public void clickButtonDialog5(View v){  
  2.     showDialog5();  
  3.    }  
  4.      
  5. public void showDialog5(){  
  6.     final Context context = this;  
  7.     //定义复选框选项  
  8.     String[] multiChoiceItems = {"Item1","Item2"};  
  9.     boolean[] defaultSelectedStatus = {false,true};//复选框默认值:false=未选;true=选中  
  10.     //创建对话框  
  11.     new AlertDialog.Builder(context)  
  12.     .setTitle("复选框")//设置对话框标题  
  13.     .setMultiChoiceItems(multiChoiceItems, defaultSelectedStatus, new OnMultiChoiceClickListener(){  
  14.         @Override  
  15.         public void onClick(DialogInterface dialog, int which,  
  16.                 boolean isChecked) {  
  17.             Toast.makeText(context, "Item"+which+"'s status  is "+isChecked+".", Toast.LENGTH_LONG).show();  
  18.         }  
  19.     })  
  20.     .setPositiveButton("确定"null)//设置对话框[肯定]按钮  
  21.     .setNegativeButton("取消"null)//设置对话框[否定]按钮  
  22.     .show();  
  23. }  
 

 

图六效果具体代码

 

main.xml布局配置

Xml代码   收藏代码
  1. <Button    
  2.     android:id="@+id/idButtonDialog6"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"   
  5.     android:text="Dialog6"  
  6.     android:onClick="clickButtonDialog6"  
  7.     />  
 

 

 

Activity代码

Java代码   收藏代码
  1. public void clickButtonDialog6(View v){  
  2.     showDialog6();  
  3.    }  
  4.      
  5. public void showDialog6(){  
  6.     final Context context = this;  
  7.     //定义列表选项  
  8.     String[] items = {"Item1","Item2"};  
  9.     //创建对话框  
  10.     new AlertDialog.Builder(context)  
  11.     .setTitle("列表")//设置对话框标题  
  12.     .setItems(items, new OnClickListener(){  
  13.         @Override  
  14.         public void onClick(DialogInterface dialog, int which) {  
  15.             Toast.makeText(context, "Item is "+which+".", Toast.LENGTH_LONG).show();  
  16.         }  
  17.     })  
  18.     .setNegativeButton("取消"new OnClickListener(){  
  19.         @Override  
  20.         public void onClick(DialogInterface dialog, int which) {  
  21.             Toast.makeText(context, "Button is "+which+" click.", Toast.LENGTH_LONG).show();  
  22.         }  
  23.     })//设置对话框[否定]按钮  
  24.     .show();  
  25. }  
 

 

图七效果具体代码

 

main.xml布局配置

Xml代码   收藏代码
  1. <Button    
  2.     android:id="@+id/idButtonDialog7"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"   
  5.     android:text="Dialog7"  
  6.     android:onClick="clickButtonDialog7"  
  7.     />  
 

另外需要布局配置文件 main_dialog_custom.xml ,内容:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:id="@+id/customDialog"  
  7.     >  
  8. <TextView    
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="姓名:"  
  12.     />  
  13. <EditText    
  14.     android:layout_width="wrap_content"   
  15.     android:layout_height="wrap_content"   
  16.     android:text=""  
  17.     android:minWidth="200dip"  
  18.     android:id="@+id/dialogCustomEditTextUserName"  
  19.     />  
  20. </LinearLayout>  
 

 

 

 

Activity代码

Java代码   收藏代码
  1. public void clickButtonDialog7(View v){  
  2.     showDialog7();  
  3.    }  
  4.      
  5.   
  6. public void showDialog7(){  
  7.     final Context context = this;  
  8.       
  9.     //初始化自定义布局参数  
  10.     LayoutInflater layoutInflater = getLayoutInflater();  
  11.     //为了能在下面的OnClickListener中获取布局上组件的数据,必须定义为final类型.  
  12.     final View customLayout = layoutInflater.inflate(R.layout.main_dialog_custom, (ViewGroup)findViewById(R.id.customDialog));  
  13.       
  14.     //创建对话框  
  15.     new AlertDialog.Builder(context)  
  16.     .setTitle("自定义布局")//设置对话框标题  
  17.     .setView(customLayout)//为对话框添加组件  
  18.     .setPositiveButton("确定"new OnClickListener(){  
  19.         @Override  
  20.         public void onClick(DialogInterface dialog, int which) {  
  21.             //获取自定义布局上的输入框的值  
  22.             EditText customDialogUserName = (EditText)customLayout.findViewById(R.id.dialogCustomEditTextUserName);  
  23.             Toast.makeText(context, "姓名="+customDialogUserName.getText().toString(), Toast.LENGTH_SHORT).show();  
  24.         }  
  25.     })//设置对话框[肯定]按钮  
  26.     .setNegativeButton("取消"null)//设置对话框[否定]按钮  
  27.     .show();  
  28. }  
 

 

图八效果具体代码

 

说明:图八和图七理论上一样的,都是自定义布局对话框。不过,图八方式可以用来替换手机的系统选项菜单风格。

 

main.xml配置

Xml代码   收藏代码
  1. <Button    
  2.     android:id="@+id/idButtonDialog8"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"   
  5.     android:text="Dialog8"  
  6.     android:onClick="clickButtonDialog8"  
  7.     />  
 

menu_gridview.xml配置

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <GridView  
  8.     android:id="@+id/gridview"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="fill_parent"  
  11.     android:numColumns="4"  
  12.     android:verticalSpacing="10dip"  
  13.     android:horizontalSpacing="10dip"  
  14.     android:stretchMode="columnWidth"  
  15.     android:gravity="center"  
  16.     />  
  17. </LinearLayout>  

 

menu_item.xml配置

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/RelativeLayout_Item"  
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content"  
  5.     android:paddingBottom="5dip"  
  6.     >  
  7.     <ImageView   
  8.         android:id="@+id/item_image"  
  9.         android:layout_centerHorizontal="true"   
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         />  
  13.     <TextView   
  14.         android:layout_below="@id/item_image"   
  15.         android:id="@+id/item_text"  
  16.         android:layout_centerHorizontal="true"   
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"   
  19.         />  
  20. </RelativeLayout>  

 

Activity代码

Java代码   收藏代码
  1.    //定义选项菜单  
  2.    private String[] allOptionsMenuTexts = {"删除","保存","帮助","添加","详细","发送","电话","照相"};  
  3.    private int[] allOptionsMenuOrders = {5,2,6,1,4,3,7,8};  
  4.    private int[] allOptionsMenuIds = {Menu.FIRST+1,Menu.FIRST+2,Menu.FIRST+3,Menu.FIRST+4,Menu.FIRST+5,Menu.FIRST+6,Menu.FIRST+7,Menu.FIRST+8};  
  5.    private int[] allOptionsMenuIcons = {  
  6.         android.R.drawable.ic_menu_delete,  
  7.         android.R.drawable.ic_menu_edit,  
  8.         android.R.drawable.ic_menu_help,  
  9.         android.R.drawable.ic_menu_add,  
  10.         android.R.drawable.ic_menu_info_details,  
  11.         android.R.drawable.ic_menu_send,  
  12.         android.R.drawable.ic_menu_call,  
  13.         android.R.drawable.ic_menu_camera  
  14.         };  
  15.   
  16.    @Override  
  17.    public void onCreate(Bundle savedInstanceState) {  
  18.        super.onCreate(savedInstanceState);  
  19.        setContentView(R.layout.main_dialog);  
  20.    }  
  21.      
  22.   
  23.      
  24.    public void clickButtonDialog8(View v){  
  25.     showDialog8();  
  26.    }  
  27.      
  28.   
  29. public void showDialog8(){  
  30.     final Context context = this;  
  31.       
  32.     //获取自定义布局  
  33.     LayoutInflater layoutInflater = getLayoutInflater();  
  34.     View menuView = layoutInflater.inflate(R.layout.menu_gridview, null);  
  35.       
  36.     //获取GridView组件并配置适配器  
  37.     GridView gridView = (GridView)menuView.findViewById(R.id.gridview);  
  38.     SimpleAdapter menuSimpleAdapter = createSimpleAdapter(allOptionsMenuTexts,allOptionsMenuIcons);  
  39.     gridView.setAdapter(menuSimpleAdapter);  
  40.     gridView.setOnItemClickListener(new OnItemClickListener(){  
  41.         @Override  
  42.         public void onItemClick(AdapterView<?> parent, View view,  
  43.                 int position, long id) {  
  44.             Toast.makeText(context, "菜单["+allOptionsMenuTexts[position]+"]点击了.", Toast.LENGTH_SHORT).show();  
  45.         }  
  46.     });  
  47.       
  48.     //创建对话框并显示  
  49.     new AlertDialog.Builder(context).setView(menuView).show();  
  50. }  
  51.   
  52. public SimpleAdapter createSimpleAdapter(String[] menuNames,int[] menuImages){  
  53.     List<Map<String,?>> data = new ArrayList<Map<String,?>>();  
  54.     String[] fromsAdapter = {"item_text","item_image"};  
  55.     int[] tosAdapter = {R.id.item_text,R.id.item_image};  
  56.     for(int i=0;i<menuNames.length;i++){  
  57.         Map<String,Object> map = new HashMap<String,Object>();  
  58.         map.put(fromsAdapter[0], menuNames[i]);  
  59.         map.put(fromsAdapter[1], menuImages[i]);  
  60.         data.add(map);  
  61.     }  
  62.       
  63.     SimpleAdapter SimpleAdapter = new SimpleAdapter(this, data, R.layout.menu_item, fromsAdapter, tosAdapter);  
  64.     return SimpleAdapter;  
  65. }  
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值