Android自定义对话框
2015-03-20 18:05
288 查看
1.作用
自定义弹出的对话框,方便对对话框进行各种操作和美化对话框
2.实现步骤
2.1在layout目录下创建dialog_setup.xml文件 2.2在创建对话框的位置引用自定义的对话框
3.效果图
4.具体实现
4.1 在layout目录下创建dialog_setup.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:background="#ffffff" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:background="#8866ff00" android:gravity="center" android:text="设置密码" android:textColor="#000000" android:textSize="18sp" /> <EditText android:hint="请输入密码" android:textColor="#000000" android:id="@+id/et_password" android:layout_width="280dp" android:layout_height="wrap_content" android:inputType="textPassword" > </EditText> <EditText android:hint="请确认密码" android:textColor="#000000" android:id="@+id/et_password_confirm" android:layout_width="280dp" android:layout_height="wrap_content" android:inputType="textPassword" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_ok" android:textColor="#000000" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定" /> <Button android:id="@+id/btn_cancel" android:textColor="#000000" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout>
4.2 在创建对话框的位置引用自定义的对话框
protected void showSetupDialog() { AlertDialog.Builder builder=new Builder(this); View view=View.inflate(getApplicationContext(), R.layout.dialog_setup, null); // 加载控件 final EditText et_password=(EditText) view.findViewById(R.id.et_password); final EditText et_password_confirm=(EditText) view.findViewById(R.id.et_password_confirm); Button btn_ok=(Button) view.findViewById(R.id.btn_ok); Button btn_cancel=(Button) view.findViewById(R.id.btn_cancel); // 取消按钮的点击事件 btn_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 隐藏dialog dialog.dismiss(); } }); btn_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String password=et_password.getText().toString().trim(); String password_confirm=et_password_confirm.getText().toString().trim(); if(!TextUtils.isEmpty(password)){ if(password.equals(password_confirm)){ // 密码设置成功 Editor edit = sp.edit(); edit.putString("password", MD5Utils.digest(password)); edit.commit(); dialog.dismiss(); //edit.apply() }else{ Toast.makeText(getApplicationContext(), "两次密码不一致", 0).show(); } }else{ Toast.makeText(getApplicationContext(), "密码不能为空", 0).show(); } } }); builder.setView(view);// 设置dialog显示的view对象 dialog = builder.create(); dialog.show(); }
兼容低版本 **
//builder.setView(view);// 设置dialog显示的view对象 dialog = builder.create(); dialog.setView(view, 0, 0, 0, 0); dialog.show();
相关文章推荐
- Android---功能模块一之颜色选择器自定义Preference 对话框
- Android自定义对话框列表
- Android自定义对话框的大小
- Android对话框的自定义及透明效果
- android自定义大小对话框
- android 自定义对话框 背景透明
- Android自定义Dialog对话框
- Android自定义对话框
- android 自定义对话框 背景透明
- android自定义对话框
- 20. android dialog——自定义对话框之一
- Android简明开发教程十八:自定义对话框 Transform
- Android简明开发教程十八:自定义对话框 Transform
- Android 对话框进阶(自定义)
- android自定义对话框
- Android Dialog( 自定义对话框)
- Android简明开发教程十八:自定义对话框 Transform
- android 自定义对话框
- android Dialog 各种Dialog 三个选项的、进度条的、单选的、多选的、自定义的对话框
- android Dialog 各种Dialog 三个选项的、进度条的、单选的、多选的、自定义的对话框