开发者

Disable keyboard input on Android TimePicker

开发者 https://www.devze.com 2022-12-21 22:29 出处:网络
I\'m developing and Android app using开发者_StackOverflow中文版 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 on

I'm developing and Android app using开发者_StackOverflow中文版 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?


try to use:

myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

to disable focus on the text views of the internal NumberPickers


Add this in your XML:

android:descendantFocusability="blocksDescendants"

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.


XML:

<NumberPicker
    ...
    android:descendantFocusability="blocksDescendants" />


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:

First, you should call setInputType(InputType.TYPE_NULL) on the editText.

Next instantiate the 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):

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.

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.


I just tried (targeting 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

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."


You can do this:

    InputMethodManager imm = 
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Please refer to this question as well:

Close/hide the Android Soft Keyboard


This does not work 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.

0

精彩评论

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

关注公众号