Disable soft keyboard on NumberPicker

前端 未结 11 1112
庸人自扰
庸人自扰 2020-12-22 18:20

I\'m trying to deactivate the soft keyboard when using a NumberPicker to enter numerical values (for aesthetic reasons). This is my layout-xml-code:



        
相关标签:
11条回答
  • 2020-12-22 18:58

    Just found this and it works like a charm:

    myNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

    You can also set this in XML:

    android:descendantFocusability="blocksDescendants" 
    
    0 讨论(0)
  • 2020-12-22 19:01

    Working code Programatically :

    mp.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    

    XML:

    android:descendantFocusability="blocksDescendants" 
    
    0 讨论(0)
  • 2020-12-22 19:05

    After reading through the com/android/internal/widget/NumberPicker.java source code i got to the following solution:

    // Hide soft keyboard on NumberPickers by overwriting the OnFocusChangeListener
    OnFocusChangeListener fcl = new OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            // Do nothing to suppress keyboard
        }
    };
    
    ((EditText) numberPicker.getChildAt(1)).setOnFocusChangeListener(fcl);
    
    // Suppress soft keyboard from the beginning
    ((EditText) numberPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);
    
    0 讨论(0)
  • 2020-12-22 19:05

    The simplest I found to work was :

    numberPicker               = (NumberPicker) myDialogView.findViewById(R.id.myViewId);
    EditText numberPickerChild = (EditText) numberPicker.getChildAt(0);
    numberPickerChild.setFocusable(false);
    numberPickerChild.setInputType(InputType.TYPE_NULL);
    
    0 讨论(0)
  • 2020-12-22 19:11

    I don't know why it works, but setting OnClickListener which does nothing prevented keyboard from showing (Lollipop)

    numberPicker.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      }
    });
    
    0 讨论(0)
提交回复
热议问题