AlertDialog简析

来源:互联网 发布:mac mini显示器接口 编辑:程序博客网 时间:2024/06/04 19:44



AlertDialog.builer创建对话框。

常用方法:


setTitle  设置标题
setIcon   设置图标
setMessage  设置内容
setItems  设置对话框中显示的项目列表
setView   设置自定义样式

setSingleChoiceItems 设置对话框显示一个单选框
setMultiChoiceItems 设置对话框显示一系列的复选框

setPositiveButton 对话框添加确定按钮
setNegativeButton 对话框添加取消按钮
setNeutralButton 普通按钮
create   创建对话框
show   显示对话框



简单的AlertDialog对话框

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Dialog alertDialog = new AlertDialog.Builder(this)     //创建对话框   .setTitle("简单对话框")    //设置标题   .setMessage("hello")    //设置内容   .setIcon(R.drawable.ic_launcher)  //设置图标   .create();     //完成对话框的创建alertDialog.show();     //显示对话框 }}


效果图:





带按钮的AlertDialog对话框

 在上面加几个Button,实现删除操作的提示对话框

     setPositiveButton
     setNegativeButton
     setNeutralButton

 

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Dialog alertDialog = new AlertDialog.Builder(this).setTitle("确定删除?").setMessage("您确定删除这条消息吗?").setIcon(R.drawable.ic_launcher).setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消按钮@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).setNeutralButton("查看详情", new DialogInterface.OnClickListener() {//添加普通按钮@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).create();alertDialog.show();}}


效果图:





类似ListView的AlertDialog对话框

//核心方法setItems(CharSequence[] items, final OnClickListener listener)

//参数1,要显示的数据的数组

//参数2,点击某个item的触发事件

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final String[] arrayWarringStates = new String[] { "齐", "楚", "秦", "燕", "赵", "魏", "韩" };Dialog alertDialog = new AlertDialog.Builder(this).setTitle("你喜欢哪个国家?").setIcon(R.drawable.ic_launcher).setItems(arrayWarringStates, new DialogInterface.OnClickListener() { //设置对话框中显示的项目列表@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this,"您加入了"+arrayWarringStates[which]+"国", Toast.LENGTH_SHORT).show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).create();alertDialog.show();}}

效果图:

 



类似RadioButton的AlertDialog

//核心方法setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)

//参数1,要显示的数据的数组

//参数2,初始值(初始被选中的item),

//参数3,点击某个item的触发事件

public class MainActivity extends Activity {private int selectedCountryIndex = 0;//默认选择项@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final String[] arrayWarringStates = new String[] { "齐", "楚", "秦", "燕","赵", "魏", "韩" };Dialog alertDialog = new AlertDialog.Builder(this).setTitle("你喜欢哪个国家?").setIcon(R.drawable.ic_launcher)//设置对话框显示一个单选框.setSingleChoiceItems(arrayWarringStates, selectedCountryIndex,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {selectedCountryIndex = which;}}).setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this,"您加入了"+arrayWarringStates[selectedCountryIndex]+"国",Toast.LENGTH_SHORT).show();// TODO Auto-generated method stub}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).create();alertDialog.show();}}

效果图:

 




类似CheckBox的AlertDialog对话框

//核心方法setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)

//参数1,要显示的数据的数组,

//参数2,选中状态的数组,

//参数3,点击某个item的触发事件

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final String[] arrayWarringStates = new String[] { "齐", "楚", "秦", "燕","赵", "魏", "韩" };
//选项初始化        final boolean[] selectedCountryIndex = new boolean[] {true, true, false, false, false, false, false};         Dialog alertDialog = new AlertDialog.Builder(this).setTitle("你喜欢哪个国家?").setIcon(R.drawable.ic_launcher)//设置对话框显示一系列的复选框 .setMultiChoiceItems(arrayWarringStates,selectedCountryIndex,new  DialogInterface.OnMultiChoiceClickListener() {                                       @Override                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {                     selectedCountryIndex[which] = isChecked;                     }                 }).setPositiveButton("确认", new DialogInterface.OnClickListener() {                    @Override                     public void onClick(DialogInterface dialog, int which) {                         StringBuilder stringBuilder = new StringBuilder(); //判断复选结果                        for (int i = 0; i < selectedCountryIndex.length; i++) {                             if (selectedCountryIndex[i] == true)                             { //字符串,用”、”把选项连接起来                                stringBuilder.append(arrayWarringStates[i] + "、");                            }                         }                         Toast.makeText(MainActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();                     }                 })                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                      @Override                     public void onClick(DialogInterface dialog, int which) {                         // TODO Auto-generated method stub                      }                 })                .create();         alertDialog.show();     } }


效果图:


 




自定义View的AlertDialog

//核心方法setView

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 取得自定义View// 动态加载布局生成View对象LayoutInflater layoutInflater = LayoutInflater.from(this);            View myloginView = layoutInflater.inflate(R.layout.activity_main, null);Dialog alertDialog = new AlertDialog.Builder(this).setTitle("用户登录").setIcon(R.drawable.ic_launcher).setView(myloginView).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).setPositiveButton("登陆", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).create();alertDialog.show();}}

效果图:

 

 



另外也可以使用内部类来封装各按钮的点击事件



0 0
原创粉丝点击