Disable keyboard input on Android TimePicker

后端 未结 7 1023
灰色年华
灰色年华 2020-12-08 20:12

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

相关标签:
7条回答
  • 2020-12-08 20:24

    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

    0 讨论(0)
  • 2020-12-08 20:26

    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 讨论(0)
  • 2020-12-08 20:28

    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.

    0 讨论(0)
  • 2020-12-08 20:29

    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.

    0 讨论(0)
  • 2020-12-08 20:29

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

    0 讨论(0)
  • 2020-12-08 20:37

    try to use:

    myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
    

    to disable focus on the text views of the internal NumberPickers

    0 讨论(0)
提交回复
热议问题