从 PreferenceScreen 上包含的 EditTextPreference 重新打开对话框
我正在尝试检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,经过几个小时的搜索,我放弃了并创建了部分解决方案。我创建了一个对话框来通知用户并在每次失败时恢复默认值。
这是我所做的:
请注意,您必须注册侦听器并取消注册 onStop。
为了创建/定义对话框,我也做了:
参考文献:
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:
Notice that you must register the listener and unregister onStop.
To create/define the dialog I also did:
References:
http://developer.android.com/guide/topics/ui/dialogs.html
http://developer.android.com/reference/android/content/SharedPreferences.html