AlterDialog 常用的样式

jopen 9年前

使用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(); 



</div>
</div>