为什么不创建自己的DialogPreference?以下是步骤:
1)用XML创建对话框的布局.
2)使用您自己的自定义类扩展android框架的DialogPreference类.
3)在构造函数中,设置对话框的布局
public CustomDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.preference_dialog_number_picker);
}
Run Code Online (Sandbox Code Playgroud)
4)覆盖onBindDialogView(View视图)方法并将布局元素绑定到代码中.从SharedPreferences文件获取当前密码值并使用TextChangedListeners处理验证,例如:如果用户未确认新密码,则显示错误.
@Override
protected void onBindDialogView(View view) {
// bind view
mOldPass = (TextView) view.findViewById(R.id.old_pass);
mNewPass = (TextView) view.findViewById(R.id.new_pass);
mConfirmPass = (TextView) view.findViewById(R.id.confirm_pass);
// get the current password from SharedPreferences
// add text changed listener for handling validation
}
Run Code Online (Sandbox Code Playgroud)
5)覆盖onClick(DialogInterface对话框,int which)方法.当用户单击对话框的按钮时,将调用此方法,例如"OK"-Button.将新密码保存在SharedPreferences文件中.
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// save your new password here
break;
default:
// do something else...
break;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3116 次 |
| 最近记录: |