在 Android TimePicker 上禁用键盘输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2391216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable keyboard input on Android TimePicker
提问by ashughes
I'm developing and Android app using the 1.6 SDK. I'm using a TimePicker
and I don't want the soft keyboard to come up when you click on the digits in the TimePicker
. I only want the TimePicker
to be changed using the plus and minus buttons. I've tried using android:focusable="false"
and android:focusableInTouchMode="false"
hoping those would do it, but they didn't seem to do anything. Is there a way to do this?
我正在使用 1.6 SDK 开发 Android 应用程序。我正在使用 aTimePicker
并且我不希望当您单击TimePicker
. 我只想TimePicker
使用加号和减号按钮更改 。我试过使用android:focusable="false"
并android:focusableInTouchMode="false"
希望那些会这样做,但他们似乎没有做任何事情。有没有办法做到这一点?
回答by user499318
try to use:
尝试使用:
myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
to disable focus on the text views of the internal NumberPickers
禁用对内部 NumberPickers 的文本视图的关注
回答by IntelliJ Amiya
Another Way . Add this in your XML:
其它的办法 。在您的 XML 中添加:
android:descendantFocusability="blocksDescendants"
android:descendantFocusability="blocksDescendants"
回答by Andrey
XML:
XML:
<NumberPicker
...
android:descendantFocusability="blocksDescendants" />
回答by Laurent
Don't know if this works for TimePicker, but I found the correct paramter to use to prevent the keyboard from displaying when focusing an edit text or using the copy and paste menu on its contents:
不知道这是否适用于 TimePicker,但我找到了正确的参数,用于防止在聚焦编辑文本或对其内容使用复制和粘贴菜单时显示键盘:
First, you should call setInputType(InputType.TYPE_NULL)
on the editText
.
首先,你应该叫setInputType(InputType.TYPE_NULL)
上editText
。
Next instantiate the InputTypeManager:
接下来实例化 InputTypeManager:
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
And then, call hideSoftInputFromWindow()
on the imm
object, but using the InputMethodManager.HIDE_NOT_ALWAYS
as second parameter instead of 0 (as advised in first answer):
然后,调用hideSoftInputFromWindow()
该imm
对象,但使用InputMethodManager.HIDE_NOT_ALWAYS
作为第二个参数而不是 0(如第一个答案中所建议的):
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS)
This will prevent the soft keyboard from ever popping up when clicking/focusing this EditText control, even when using the menu key to perform copy/paste operations.
这将防止在单击/聚焦此 EditText 控件时弹出软键盘,即使在使用菜单键执行复制/粘贴操作时也是如此。
PS: if you want to prevent copy/paste operations you can call editText.setEditable(false)
but then you won't be able to dynamically change the EditText's contents.
PS:如果您想阻止复制/粘贴操作,您可以调用,editText.setEditable(false)
但您将无法动态更改 EditText 的内容。
回答by samis
I just tried (targeting 4.0.3)
我刚试过(针对 4.0.3)
myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
with no success. After searching google for "android TimePicker BLOCK_DESCENDANTS not working" i followed the hit
没有成功。在谷歌搜索“android TimePicker BLOCK_DESCENDANTS not working”后,我跟着点击
http://code.google.com/p/android/issues/detail?id=38369
where the last post, which is by a "Project Member", states:
由“项目成员”撰写的最后一篇文章指出:
"Descendant focusability refers to a view's ability to take focus, as in focus traversal using a keyboard, trackball, or d-pad. It does not affect touch events by design.
“后代可聚焦性是指视图获得焦点的能力,如使用键盘、轨迹球或方向键进行焦点遍历。它不会影响设计上的触摸事件。
If you need to block touch events from a descendant view, consider overriding onInterceptTouchEvent in a parent view and returning true when blocking touch events is desired. This will cause the MotionEvents to be sent to the intercepting parent until the last pointer goes up."
如果您需要阻止来自后代视图的触摸事件,请考虑覆盖父视图中的 onInterceptTouchEvent 并在需要阻止触摸事件时返回 true。这将导致 MotionEvents 被发送到拦截父对象,直到最后一个指针上升。”
回答by MexicanHacker
You can do this:
你可以这样做:
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Please refer to this question as well:
也请参考这个问题:
回答by Laurent
This does notwork either for EditText controls... If you press & hold the Menu key on an EditText on which you have set InputType.TYPE_NULL
and called immhideSoftInputFromWindow(...)
, the soft keyboard will still pop up along with the copy/paste menu options.
这并没有对EditText上控制工作之一......如果你按住上您已设置一个EditText菜单键InputType.TYPE_NULL
和称为immhideSoftInputFromWindow(...)
,软键盘仍然会弹出与复印件一并/粘贴菜单选项。