Android中对话框(Dialog)的创建方法
1,创建
new AlertDialog.Builder(Context context)
一些设置:
create()创建
show()显示
dismiss()退出对话框
2,常用方法
setIcon:设置图标
setTitle:设置标题
setPositiveButton:设置 确定按钮
setNegativeButton:设置 取消按钮
setNeutralButton:设置 忽略按钮
setCancelable(Boolean arg0)//按回退键是否可以取消
3,可选但唯一的方法
setMessage 设置显示消息
setItems 显示单选列表,选择后对话框退出
setSingleChoiceItems 显示单选列表,选择后对话框不退出
setMultiChoiceItems 显示多选列表,选择后对话框不退出
4,从资源文件加载对框视图
通过setView( LayoutInflater.from(Context context).inflate(resource, null) )安装视图
5,通过Activity.showDialog(int id)来显示对话框
重写Activity的以下方法
protected Dialog onCreateDialog(int id) //Dialog第一次创建时调用.这里需要通过AlertDialog.create()返回创建的对话框
protected onPrepareDialog(int id, Dialog dialog) //Dialog显示前调用
6,其他对话框
ProgressDialog对话框
setIndeterminate 设置进度条是否自动运转
setProgressStyle 设置显示风格;
ProgressDialog.STYLE_HORIZONTAL/ProgressDialog.STYLE_SPINNER
setProgress 设置进度,只有对话框显示后才有用
setCancelable(Boolean arg0)//按回退键是否可以取消,进度条对框框默认不取消
setMessage() 设置显示内容
最简单的显示方法:
public static ProgressDialog show (Context context, CharSequence title, CharSequence message)
TimePickerDialog/DatePickerDialog
例子:“从配置文件加载对话框视图”
public void inputPwd() {
//LayoutInflater是用来找layout下xml布局文件,并且实例化LayoutInflater inflater = LayoutInflater.from(Main.this);final View layView = inflater.inflate(R.layout.dialogview, null);//加载配置文件来创建一个对话框Builder dialog = new AlertDialog.Builder(Main.this).setView(layView); dialog.setTitle("输入密码"); //添加 " OK " 按钮的单击事件 dialog.setPositiveButton(" OK ", new DialogInterface.OnClickListener() { @Overridepublic void onClick(DialogInterface dialog, int which) { //必须在findViewById()方法之前写上layView,否则默认为main.xml EditText secondPwd = (EditText) layView.findViewById(R.id.secondPwd); String inputPwd = secondPwd.getText().toString(); //从对话框中获取到的数据赋值给inputPwd String pwdDB = getPassWord(); //从数据库中获取密码,并将其赋值给pwdDB if (inputPwd.equals(pwdDB)) { //如果输入的密码等于数据库中的密码,返回首页 //返回到首页 Intent intent = new Intent(Intent.ACTION_MAIN); startActivity(intent); }}
})
.setNeutralButton(“CANCEL”, new DialogInterface.OnClickListener() {
@Overridepublic void onClick(DialogInterface dialog, int which) { System.out.println("++ CANCEL ++");}
})
.create();
dialog.show();
}
更多相关文章
- Android窗口机制之由setContentView引发的Window,PhoneWindow,Deco
- Android完全关闭应用程序
- Android官方架构组件ViewModel+LiveData+DataBinding架构属于自
- Android通过访问网页查看网页源码实例详解
- Android(安卓)MediaPlayer 字幕同步
- Android(安卓)8.1 来电默认全屏显示 如何修改
- Android(安卓)Studio finish()方法的使用与解决app点击“返回”,
- Android(安卓)EventBus 架构设计
- My Android成长之路(二)——【JSON】