开发者

Preference onClick

I made a settings menu for my app but was wondering how would i go about starting a activity when a Preference is clicked in the menu? I want the user to be able to click on a menu bar saying "Change password" and for it to start another activity so they can enter their p开发者_开发知识库assword, enter a new password, and comfirm that new password so it will change the sharedpreferences password file. If theres anyother easier way to do this I would greatly appreciate the input. Thanks


Why not just creating your own DialogPreference? Here are the steps:

1) create the layout for your dialog in XML.

2) Extend the DialogPreference class of the android framework with your own custom class.

3) In the constructor, set the layout of the dialog with

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

4) override the onBindDialogView(View view) method and bind your layout elements to your code. Get your current password value from the SharedPreferences-file and handle validation with TextChangedListeners, for example: if the user does not confirm the new password, show an error.

@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) override the onClick(DialogInterface dialog, int which) method. this method will be invoked, when a user clicked a button of the dialog, for example the "OK"-Button. Save the new password in the SharedPreferences-file.

@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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜