AlterDialog 经常使用的样式

Posted mthoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AlterDialog 经常使用的样式相关的知识,希望对你有一定的参考价值。

使用AlerDialog 创建对话框 :

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

1.设置简单的对话框

builder.setIcon(R.drawable.icon);  
       builder.setTitle("你确定要离开吗?");  
       builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
           public void onClick(DialogInterface dialog, int whichButton) {  
               //这里加入点击确定后的逻辑  
           }  
       });  
       builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
           public void onClick(DialogInterface dialog, int whichButton) {  
               //这里加入点击确定后的逻辑  
           }  
       });  
       builder.create().show();  

2.设置列表对话框

String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"}; 
AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
        builder.setTitle("列表选择框");  
        builder.setItems(mItems, new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int which) {  
                //点击后弹出窗体选择了第几项  
            }  
        });  
        builder.create().show();  

3.设置单选对话框

int mSingleChoiceID = -1;  
builder.setIcon(R.drawable.icon);  
    builder.setTitle("单项选择");  
    builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
                mSingleChoiceID = whichButton;  
        }  
    });  
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            if(mSingleChoiceID > 0) {  
          //  你选择的是" + mSingleChoiceID;  
            }  
        }  
    });  
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
 
        }  
    });  
   builder.create().show(); 

4.设置多选对话框

ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>(); 
builder.setIcon(R.drawable.icon);  
    builder.setTitle("多项选择");  
    builder.setMultiChoiceItems(mItems,  
            new boolean[]{false, false, false, false, false, false, false},  
            new DialogInterface.OnMultiChoiceClickListener() {  
                public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {  
                   if(isChecked) {  
                       MultiChoiceID.add(whichButton);  
                  //   你选择的为:mItems[whichButton] 
                   }else {  
                       MultiChoiceID.remove(whichButton);  
                   }   
                }  
            });  
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            String str = "";  
            int size = MultiChoiceID.size();  
            for (int i = 0 ;i < size; i++) {  
          <span style="white-space:pre">	</span>  str+= mItems[MultiChoiceID.get(i)] + ", ";  
            }  
        }  
    });  
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
 
        }  
    });  
   builder.create().show(); 

5.设置自己定义布局对话框

     View view =  LayoutInflater.from(this).inflate(R.layout.custom, null);  
     builder.setIcon(R.drawable.icon);  
     builder.setTitle("自己定义输入框");  
     builder.setView(<span style="font-family: 宋体, ‘Arial Narrow‘, arial, serif;">view</span>);  
     builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
         public void onClick(DialogInterface dialog, int whichButton) {  
           
         EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);  
         EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);  
         // userName.getText().toString()  +  password.getText().toString();  
         }  
     });  
     builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
         public void onClick(DialogInterface dialog, int whichButton) {  
 
         }  
     });  
   builder.create().show(); 





以上是关于AlterDialog 经常使用的样式的主要内容,如果未能解决你的问题,请参考以下文章

Android之AlterDialog介绍

js经常用到的代码片段

Android-Dialog

C#程序员经常用到的10个实用代码片段

C#程序员经常用到的10个实用代码片段 - 操作系统

请问在android中,下面两张图片最上方的那个弹出框是怎么实现的,貌似不是AlterDialog popWindow之类的