从 PreferenceScreen 上包含的 EditTextPreference 重新打开对话框

发布于 2024-12-02 14:10:48 字数 990 浏览 2 评论 0原文

我正在尝试检查 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) 获取对话框,但由于运行时它不再显示,我得到一个空指针:(

另外,我不确定这是否是检查输入格式和的最佳方法小时和分钟,

谢谢!

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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2024-12-09 14:10:48

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

这是我所做的:

@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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文