Skip to content Skip to sidebar Skip to footer

How To Call The Ok Button In The Edittextpreference

I have an EditTextPreference in the PreferenceActivity. When user click the EditTextPreference will show a dialog. In the dialog, user can input a value, and the dialog has 'OK'

Solution 1:

You can extend EditTextPreference to get control over the click handler.

package myPackage;
publicclassCustomEditTextPreferenceextendsEditTextPreference {

    publicCustomEditTextPreference(Context context) {
        super(context);
    }

    publicCustomEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    publicCustomEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @OverridepublicvoidonClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
            // add Handler here
        }
        super.onClick(dialog, which);
    }

}

In the Xml instead of <EditTextPreference/> reference it like this:

<myPackage.CustomEditTextPreferenceandroid:dialogTitle="Registration Key"android:key="challengeKey"android:title="Registration Key"android:summary="Click here to enter the registration key you received by email."/>

Solution 2:

Actually you can't since the preference is using an internal AlertDialog.Builder and creates a new dialog every time you click the preference. The next problem is that the dialog builder sets the click listener for you and if you override them you might destroy the close behavior of the button click.

This bothered me since I wanted a preference which only closes on valid input (otherwise a toast is shown and user should press cancel if he can't get it right).

(If you really need a solution for exactly this problem) You can find general solution of a validating DialogPreferencehere and a validating EditTextPreferencehere which I wrote myself.

Solution 3:

Your preference activity doesn't appear to be implementing a

OnSharedPreferenceChangeListener

You may want to read over the excellent answer to the question: Updating EditPreference

Post a Comment for "How To Call The Ok Button In The Edittextpreference"