views:

487

answers:

2

I would like to be able to close the editpreference dialog (as shown here http://twitpic.com/18ttdp) by pressing the 'Done' button on the keyboard.

Currently, pressing 'Done' just dismisses the keyboard but leaves the dialog.

In other parts of my application I use code similar to the following to intercept the 'Done' key press and execute actions in my activity:

text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                //do stuff here
                return true;
            }
            return false;
        }
    });

However, I am unsure of how to do achieve this same effect in my preference activity or layout xml.

A: 

Can you pass the same event that happens when you hit the "OK" button on the dialog when you intercept the "Done" press?

Feet
How do I do that?
Damian
A: 

Instead of adding the listener there, you should do something similar to this:

getDialog().setOnKeyListener(new OnKeyListener() {

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        dialog.dismiss();
        return true;
    }
});

This code will dismiss the dialog when a key is pressed.

Macarse
Thanks, this dismisses the dialog but does not retain the value. If I get access to the shared preferences (SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);) within the dialog and store the value of the textfield before it is dismissed then the new value is saved. However when you click on the preference again, the old value is displayed (it is cached somehow). I have to quit out of the preferences screen and then select preferences --> pref_option in for the new saved value to be displayed. How do I fix this part?
Damian
I should warn you that I wouldn't do it. I feel that this will make your UI not intuitive. If you want to do it, try adding something like persistString(mValue); to that code.
Macarse
I used prefs.edit.putString() (persistString is a protected method). As I say it does actually store the value but the old (cached) value is displayed if I select the preference again immediately after dismissing the dialog (from the keyboard press).Is it possible to refresh the value somehow when the dialog is displayed?
Damian
@Damian: Remember that when you use a putString from an editor, you need to call commit(). Example: SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); Editor editor = sp.edit(); editor.putString("key", "value"); editor.commit();
Macarse
Thanks, yeah I did do that.
Damian
@Damian: that means it solved your issue, or it still doesn't work?
Macarse
Issue still not solved. I did use commit and the data is actually saved. Problem is that when you click on the preference again (immediately after the dialog has been dismissed) the old value is displayed. You have to close the preferences screen (i.e. navigate to another activity and back again) for the new value to be picked up.
Damian