Wyłącz miękką klawiaturę na NumberPicker

Próbuję wyłączyć miękką klawiaturę, gdy używam Numberpickera do wprowadzania wartości liczbowych (ze względów estetycznych). To jest mój layout-XML-code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp" >

        <NumberPicker
            android:id="@+id/repetitionPicker"
            android:layout_width="40dp"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/repetitions_short_divider"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <NumberPicker
            android:id="@+id/weightPicker"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/pounds"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>


    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/save" />

</LinearLayout>

I na koniec jest to kod, w którym próbuję zablokować klawiaturę w metodzie onCreate () -:

// hide keyboard
View.OnClickListener disableKeyBoardListener = new View.OnClickListener() {
    public void onClick(View v) {
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
};

((EditText) weightPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);
((EditText) repetitionPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);

((EditText) weightPicker.getChildAt(1)).setOnClickListener(disableKeyBoardListener);
//((EditText) repetitionPicker.getChildAt(1)).setOnClickListener(disableKeyBoardListener);
//weightPicker.setOnClickListener(disableKeyBoardListener);
//repetitionPicker.setOnClickListener(disableKeyBoardListener);     

getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

Niestety, miękka klawiatura nadal pojawia się po kliknięciu na NumberPicker. Jakieś pomysły?

Author: damaxxed, 2012-01-13

9 answers

Po prostu znalazłem to i działa jak czar:

myNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

Możesz również ustawić to w XML:

android:descendantFocusability="blocksDescendants" 
 413
Author: Andrew Webber,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-08 09:15:41

Wersja Xml odpowiedzi Andrew Webbera

android:descendantFocusability="blocksDescendants"

Przykład

<NumberPicker
        android:id="@+id/your_numberpicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"/>
 49
Author: G_V,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-01 11:24:37

Po przeczytaniu przez com / android / internal / widget / NumberPicker.kod źródłowy Javy znalazłem następujące rozwiązanie:

// 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);
 6
Author: damaxxed,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-17 11:19:18

Po prostu wzmocnione @ MaxVogler 's ans (więc jeśli wannt głos ten głos @ MaxVogler zbyt) i zrobić to solidny hack. Nie musimy też wywoływać setOnFocusChangeListener i setInputType. Wystarczy tylko setFocusable do false.

Poniżej znajduje się helper api do włączania / wyłączania funkcji

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;
        }
    }
}
 6
Author: havexz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-21 05:35:01

Oto inny sposób, aby to zrobić, który pozwala użytkownikowi nadal edytować numer, jeśli chce - to po prostu najpierw tłumi miękką klawiaturę. Użycie NumberPicker.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS) aby stłumić miękką klawiaturę, gdy interfejs po raz pierwszy pokazuje się zgodnie z odpowiedziami powyżej. Następnie uruchom okno dialogowe lub aktywność, aby zaimplementować View.OnTouchListener, wywołaj setOnTouchListener(this) na swoim NumberPicker, a w swojej implementacji onTouch(View v,MotionEvent e) zresetuj fokusowość potomka numberpickera do wartości normalnej, a następnie zwróć false.

Zwracanie false oznacza, że dotyk jest nadal przetwarzany przez NumberPicker, co oznacza, że jeśli użytkownik dotknie pola edycji, pojawi się miękka klawiatura. Dzieje się tak dokładnie, co chciałem w obliczu tego samego problemu - posiadanie miękkiej klawiatury wymyślić okno dialogowe, gdy po raz pierwszy pokazuje, jest nieprzyjemne, ponieważ przesuwa okno dialogowe po jego pojawieniu się.

public class GetBufferDialog extends DialogFragment implements View.OnTouchListener {

Po utworzeniu okna dialogowego w onCreateDialog() metoda i znalezienie NumberPicker:

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

A oto metoda OnTouch:

public boolean onTouch(View v, MotionEvent event) {
    m_numberpicker.setDescendantFocusability(m_oldFocus);
    return false;
}
 4
Author: Dave Haines,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-14 13:28:42

Najprościej było:

numberPicker               = (NumberPicker) myDialogView.findViewById(R.id.myViewId);
EditText numberPickerChild = (EditText) numberPicker.getChildAt(0);
numberPickerChild.setFocusable(false);
numberPickerChild.setInputType(InputType.TYPE_NULL);
 2
Author: bapjg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-15 11:48:10

Nie wiem dlaczego to działa, ale ustawienie OnClickListener, które nic nie robi, uniemożliwiło wyświetlenie klawiatury (Lollipop)

numberPicker.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
  }
});
 1
Author: user4215456,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-12 21:16:01

Jeśli chcesz ukryć klawiaturę programową tylko podczas ładowania widoku za pomocą selektora numerów, ale nadal chcesz, aby użytkownicy mogli edytować po załadowaniu widoku, nie powinieneś blokować ustawiania ostrości potomka. Zamiast tego, po prostu zapobiec selektor liczb jest pierwszym elementem skupionym w widoku.

Zobacz Ta odpowiedź Po szczegóły.

Na podstawie powyższej odpowiedzi:

    <!-- 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"/>
 1
Author: eckc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-20 19:37:07
/**
 * 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();
        }
    }
}

* wywołanie tego:

  • Nie blokuje funkcji fokusowania Potomków (numberpicker będzie edytowalny)

  • Ukryje miękkie wejście podczas tworzenia

  • Before (processing input) getValue () pozwoli na uzyskanie poprawnego walue


 0
Author: ceph3us,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-09 22:14:31