本文主要是介绍【总结】Android攻城狮之Dialog,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android攻城狮之Dialog
目录
- Android攻城狮之Dialog
- 目录
- Dialog简介
- 几种常见的Dialog
- AlertDialog常用方法
- AlertDialog实例
- 确认对话框
- 单选对话框
- 多选按钮对话框
- 列表对话框
Dialog简介
对话框是在当前界面
几种常见的Dialog
(1)确认对话框; (2)单选按钮对话框; (3)多选按钮对话框; (4)列表对话框;
AlertDialog常用方法
要创建一个AlertDialog,就要用到AlertDialog.Builder中的create0方法。
- setTitle :为对话框设置标题
- setIcon :为对话框设置图标
- setMessage: 为对话框设置内容
- setView :给对话框设置自定义样式
- setItems :设置对话框要显示的一个list,一般用于显示几个命令时
- setMultiChoiceltems: 用来设置对话框显示一系列的复选框
- setSingleChoiceItems :设置单选按钮
- setNeutralButton :普通按钮
- setPositiveButton :给对话框添加”确认”按钮
- setNegativeButton :对话框添加”取消”按钮
AlertDialog实例
确认对话框
private void initEvent(){findViewById(R.id.dialog_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {showDialog1();}});
}
private void showDialog1(){AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("确认对话框");builder.setIcon(R.drawable.ic_launcher);builder.setMessage("确认对话框提示内容");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this,"点击了确定按钮",Toast.LENGTH_LONG).show();}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this, "点击了取消按钮",Toast.LENGTH_LONG).show();}});AlertDialog dialog = builder.create();dialog.show();
}
单选对话框
String[] str = {"男生","女生","程序员"};
private void showDialog2(){AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("性别选择框");builder.setIcon(R.drawable.ic_launcher);builder.setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {String s = str[i];Toast.makeText(MainActivity.this,"我是"+s+"",Toast.LENGTH_LONG).show();}});AlertDialog dialog = builder.create();dialog.show();
}
多选按钮对话框
String[] str = {"篮球","足球","乒乓球","游泳","程序员"};
private void showDialog3(){AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("爱好选择框");builder.setIcon(R.drawable.ic_launcher);builder.setMultiChoiceItems(str, null, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i, boolean isChecked) {if(isChecked){Toast.makeText(MainActivity.this,"我喜欢上了"+str[i],Toast.LENGTH_LONG).show();}else{Toast.makeText(MainActivity.this,"我不再喜欢"+str[i],Toast.LENGTH_LONG).show();}}});AlertDialog dialog = builder.create();dialog.show();
}
列表对话框
String[] str = {"经理","策划","美工","程序员"};
private void showDialog4(){AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("部门列表框");builder.setIcon(R.drawable.ic_launcher);builder.setItems(str, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this,str[i]+"态度真差",Toast.LENGTH_LONG).show();}});AlertDialog dialog = builder.create();dialog.show();
}
这篇关于【总结】Android攻城狮之Dialog的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!