1、先定义一个xmL文件用于,用于填充成View对象;
2,得到 AlertDialog.builder对象; AlertDialog.Builder builder = new AlertDialog.Builder(this);
3,创建一个dialog; AlertDialog dialog = builder.create();
4;填充inflate View对象; View view = View.inflate(this, R.layout.dialog_set_password, null);
5;用Dialog 设置 View对象; dialog.setView(view); //将自定义的View 显示在对话框中。
6;显示 弹窗, 自定义的View dialog.show(); //要将dialog 的设置show显示。
//在低版本中会出现,带黑边的对话框,消除黑边的办法 如下,边距设为0;
dialog.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
android:inputType="textPassword" //以密码形式显示。 password 过时了;
这种方法出现的问题是,真机测试时对话框显示的尺寸很小;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.create();
// View view = View.inflate(this, R.layout.dialog_set_password, null);
// dialog.setView(view, 0, 0, 0, 0); // 将自定义的View 显示在对话框中。
dialog.show(); // 要将dialog 的设置show显示。
改变显示对话框的尺寸方法:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.create();
dialog.setContentView(R.layout.dialog_set_password);
WindowManager.LayoutParams wlp = dialog.getWindow().getAttributes(); //拿到窗口管理器 得到其属性值。
wlp.width = (int) (getWindowManager().getDefaultDisplay().getWidth()*0.8);//
wlp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(wlp);
点击取消时,dialog 的隐藏;
dialog.dimiss();
TextUtils.isEmpty(password) //判断 Returns true if the string is null or 0-length.如果字符串为null,或者长度为0.