开发者

Reopening a Dialog from a EditTextPreference included on a PreferenceScreen

I am trying to check the format of an input for an editText preference, in this case 24 hour format H:mm, and I want to force the edit dialog to appear again if there is an input format error.

My idea is using a OnPreferenceChange listener running on the Settings activity that implements the PreferenceScreen:

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        //check 24 hour format
        SharedPreferences myPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);        
        String startTime= myPreferences.getString(PREF_FLAT_RATE_START, "18:00");

        try{
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            Date time = sdf.parse(startTime);
        }catch (Exception e){ //If exception there is a format error...
            Log.v("Settings", "rateTime not properly formatted");

                ---> Re Open Dialog from EditText key = PREF_FLAT_RATE_START <--- 

        }

    }

Is it possible? I've already tried to get the dialog from the findViewByid(EDITTEXT) but as it is not showing anymore when is runned I get a null pointer :(

Also I am not sure if this is the best way to check the input format for and HOUR and MINUTE.

Tha开发者_Go百科nks!


Finally, after hours of searching I gave up and created a partial solution. I created a dialog to notify the user and restored defaults on each set FAIL.

Here is what i did:

@Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        if(key.equals(/**FINAL VALUE**/)){   
            String startTime = sharedPreferences.getString(/**FINAL VALUE**/, "18:00");
            try{
                SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
                sdf.parse(startTime);
            }catch (Exception e){
                Log.v("Settings", "error parsing /**FINAL VALUE**/");
                showDialog(DIALOG_FINAL_INT);
                //Restore the value.
                SharedPreferences myPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor myPreferencesEditor = myPreferences.edit();
                myPreferencesEditor.putString(/**FINAL VALUE**/, "18:00");
                myPreferencesEditor.commit();
            }           
        }
}

Notice that you must register the listener and unregister onStop. To create/define the dialog I also did:

protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case /**FINAL_INT**/:
            // do the work to define the Dialog
            AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
            builder1.setMessage(R.string.STRINGID)
                   .setCancelable(false)
                   .setNegativeButton("Ok", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            AlertDialog alert1 = builder1.create();
            dialog = alert1;
            break;
[...]

References:

http://developer.android.com/guide/topics/ui/dialogs.html

http://developer.android.com/reference/android/content/SharedPreferences.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜