【问题标题】:Reopening a Dialog from a EditTextPreference included on a PreferenceScreen从 PreferenceScreen 中包含的 EditTextPreference 重新打开对话框
【发布时间】:2011-09-01 11:00:45
【问题描述】:

我正在尝试检查 editText 首选项的输入格式,在本例中为 24 小时格式 H:mm,如果出现输入格式错误,我想强制再次显示编辑对话框。

我的想法是使用在实现 PreferenceScreen 的设置活动上运行的 OnPreferenceChange 侦听器:

    @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 <--- 

        }

    }

有可能吗?我已经尝试从 findViewByid(EDITTEXT) 获取对话框,但由于运行时不再显示,我得到一个空指针:(

我也不确定这是否是检查 HOUR 和 MINUTE 输入格式的最佳方法。

谢谢!

【问题讨论】:

    标签: android dialog android-edittext preferencescreen


    【解决方案1】:

    最后,经过数小时的搜索,我放弃并创建了一个部分解决方案。我创建了一个对话框来通知用户并在每组 FAIL 上恢复默认值。

    这是我所做的:

    @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();
                }           
            }
    }
    

    请注意,您必须注册监听器并取消注册 onStop。 要创建/定义我也做过的对话框:

    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;
    [...]
    

    参考资料:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多