关于android:偏好onClick | 珊瑚贝

Preference onClick


我为我的应用程序制作了一个设置菜单,但想知道当在菜单中单击首选项时我将如何开始活动?我希望用户能够单击显示”更改密码”的菜单栏并启动另一个活动,以便他们输入密码、输入新密码并确认新密码,以便更改共享首选项密码文件.如果有其他更简单的方法可以做到这一点,我将不胜感激。谢谢

  • 检查这个,stackoverflow.com/questions/5330677/…


为什么不直接创建自己的 DialogPreference?以下是步骤:

1) 在 XML 中为您的对话框创建布局。

2) 使用自己的自定义类扩展android框架的DialogPreference类。

3) 在构造函数中,用

设置对话框的布局

1
2
3
4
public CustomDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setDialogLayoutResource(R.layout.preference_dialog_number_picker);
}

4) 覆盖 onBindDialogView(View view) 方法并将您的布局元素绑定到您的代码。从 SharedPreferences 文件中获取您当前的密码值并使用 TextChangedListeners 处理验证,例如:如果用户没有确认新密码,则显示错误。

1
2
3
4
5
6
7
8
9
@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
}

5) 覆盖 onClick(DialogInterface dialog, int which) 方法。当用户单击对话框的按钮时,将调用此方法,例如”确定”按钮。将新密码保存在 SharedPreferences 文件中。

1
2
3
4
5
6
7
8
9
10
11
@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;
    }
}
  • 您实际上应该使用以下方法来处理较新 API 中的取消和确认按钮 @Override protected void onDialogClosed(boolean positiveResult) { if (positiveResult) { // Handle ok button } else { \\t// Handle cancel button } }


来源:https://www.codenong.com/7037351/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_10141.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?