Disable soft keyboard on NumberPicker

前端 未结 11 1111
庸人自扰
庸人自扰 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:44
    /**
     * set focus to top level window
     * disposes descendant focus
     * disposes softInput
     * @param context - activity context
     * @param enable - state of focus
     * */
    public static void topLevelFocus(Context context, boolean enable){
        if(Activity.class.isAssignableFrom(context.getClass())){
            ViewGroup tlView = (ViewGroup) ((Activity) context).getWindow().getDecorView();
            if(tlView!=null){
                tlView.setFocusable(enable);
                tlView.setFocusableInTouchMode(enable);
                tlView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
                tlView.requestFocus();
            }
        }
    }
    

    * calling this:

    • will not block descendant focusability (numberpicker will be editable)

    • will hide soft input on create

    • before (processing input) getValue() will allow to get proper walue


    0 讨论(0)
  • 2020-12-22 18:46

    Here's another way to do it which enables the user still to edit a number if they want to - it just suppresses the soft keyboard initially. Use NumberPicker.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS) to suppress the soft keyboard when the interface first shows as per answers above. Then get your dialog or activity to implement View.OnTouchListener, call setOnTouchListener(this) on your NumberPicker, and in your implementation of onTouch(View v,MotionEvent e) reset the numberpicker descendant focusability to its normal value, then return false.

    Returning false means that the touch is still processed by the NumberPicker, which means that if the user taps the edit box the soft keyboard comes up. This happens to be exactly what I wanted faced with the same problem - having the soft keyboard come up with the dialog when it first shows is displeasing as it shifts the dialog up after it appears.

    public class GetBufferDialog extends DialogFragment implements View.OnTouchListener {
    

    after creating the Dialog in the onCreateDialog() method and finding the NumberPicker:

    m_oldFocus = m_numberpicker.getDescendantFocusability();
    m_numberpicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
    m_numberpicker.setOnTouchListener(this);
    

    and here's the OnTouch method:

    public boolean onTouch(View v, MotionEvent event) {
        m_numberpicker.setDescendantFocusability(m_oldFocus);
        return false;
    }
    
    0 讨论(0)
  • 2020-12-22 18:48

    If you only want to hide the software keyboard when loading the view with your number picker, but still want the users to be able to edit after the view loads, then you shouldn't block descendant focusability. Instead, just prevent the number picker from being the first focused item in your view.

    See this answer for details.

    Based on the above answer:

        <!-- Dummy item to prevent Number Picker from receiving focus -->
        <LinearLayout        
            android:focusable="true" 
            android:focusableInTouchMode="true"
            android:layout_width="0px" 
            android:layout_height="0px"/>
    
        <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component 
             to prevent the dummy from receiving focus again -->
        <NumberPicker 
            android:id="@+id/number_picker"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:nextFocusUp="@id/number_picker" 
            android:nextFocusLeft="@id/number_picker"/>
    
    0 讨论(0)
  • 2020-12-22 18:50

    Xml version of Andrew Webber's answer

    android:descendantFocusability="blocksDescendants"
    

    Example

    <NumberPicker
            android:id="@+id/your_numberpicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:descendantFocusability="blocksDescendants"/>
    
    0 讨论(0)
  • 2020-12-22 18:51

    Just enhanced the @MaxVogler 's ans (so if wannt vote this vote @MaxVogler too) and make it a robust hack. Also we dont need to call setOnFocusChangeListener and setInputType. Only setFocusable to false will do.

    Below is a helper api to enable/disable the feature

    public static void enableNumberPickerManualEditing(NumberPicker numPicker,
            boolean enable) {
        int childCount = numPicker.getChildCount();
    
        for (int i = 0; i < childCount; i++) {
            View childView = numPicker.getChildAt(i);
    
            if (childView instanceof EditText) {
                EditText et = (EditText) childView;
                et.setFocusable(enable);
                return;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-22 18:57

    This extension is nice to not forget how to do it and have readable code. It is little bit hiding implementation details, but in this case I believe it's acceptable:

    fun NumberPicker.disableTextEditing(disable: Boolean) {
        descendantFocusability = if (disable) FOCUS_BLOCK_DESCENDANTS else FOCUS_BEFORE_DESCENDANTS
    }
    
    0 讨论(0)
提交回复
热议问题