133

我试图在使用NumberPicker输入数值(出于美学原因)时停用软键盘。这是我的布局的XML代码:禁用NumberPicker上的软键盘

<?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> 

最后这正是我试图阻止键盘在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); 

可悲的是,软键盘依旧当点击一个NumberPicker时出现。有任何想法吗?

回答

380

刚刚发现这一点,它的工作原理就像一个魅力:

myNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

您还可以设置这XML:

android:descendantFocusability="blocksDescendants" 
+1

除非我遗漏了一些东西,否则这将无法输入NumberPicker的值。这不一定是坏事,这取决于你如何使用选择器,但它似乎是解决方案的限制。 – frenziedherring 2013-08-26 17:01:52

+67

你也可以在XML中设置:android:descendantFocusability =“blocksDescendants” – 2013-08-29 08:21:19

+1

Aaah,blocksDescendants,ofcourse。非常感谢:) – Lev 2016-03-30 07:27:09

6

通过COM /安卓/内部/空间/ NumberPicker.java源代码,我到下面的解决方案看完:

// 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

那么它的类型的黑客吧? – havexz 2012-01-17 21:24:13

+0

是的,我不得不直接编辑NumberPicker的子视图。有时Android API非常稀少。 – damaxxed 2012-01-21 04:37:01

+0

+1找到破解... :) – havexz 2012-01-21 05:35:26

6

只是提高了@MaxVogler的ANS(所以如果wannt投这一票@MaxVogler),并使其成为一个强大的黑客。我们也不需要拨打setOnFocusChangeListenersetInputType。只有setFocusable才会做假。

下面是一个辅助的API来启用/禁用该功能

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; 
     } 
    } 
} 
3

下面是另一种方法来实现它如果用户想要,用户仍然要编辑一个号码 - 它只是最初抑制软键盘。使用NumberPicker.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS)可以在界面首次按照上述答案显示时抑制软键盘。然后让你的对话框或活动执行View.OnTouchListener,在你的NumberPicker上调用setOnTouchListener(this),并在你的onTouch(View v,MotionEvent e)执行onTouch(View v,MotionEvent e)重置numberpicker后裔聚焦能力为其正常值,然后返回false。

返回false意味着触摸仍由NumberPicker处理,这意味着如果用户点击编辑框,软键盘就会出现。这正好是我想要面对的同样的问题 - 让软键盘在第一次显示时出现对话框,令人不快,因为它在对话框出现后将它移开。

public class GetBufferDialog extends DialogFragment implements View.OnTouchListener { 

onCreateDialog()方法创建对话框,找到NumberPicker后:

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

和这里的OnTouch方法:

public boolean onTouch(View v, MotionEvent event) { 
    m_numberpicker.setDescendantFocusability(m_oldFocus); 
    return false; 
} 
+0

只有这一个帮助我解决了我的问题,谢谢你的方法 – smoothumut 2015-12-30 09:23:46

2

我发现工作最简单的是:

numberPicker    = (NumberPicker) myDialogView.findViewById(R.id.myViewId); 
EditText numberPickerChild = (EditText) numberPicker.getChildAt(0); 
numberPickerChild.setFocusable(false); 
numberPickerChild.setInputType(InputType.TYPE_NULL); 
+0

非常感谢!我尝试了一切都没有结果,但这工作正常... – Christian 2015-09-10 21:11:39

0
/** 
* 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(); 
     } 
    } 
} 

*调用此:

  • 不会阻止后代可聚焦性(numberpicker会编辑)

  • 将隐藏软输入(先处理输入创建

  • )getValue()将允许获得正确的walue


47

安德鲁·韦伯的回答

android:descendantFocusability="blocksDescendants" 

实例的XML版本

<NumberPicker 
     android:id="@+id/your_numberpicker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:descendantFocusability="blocksDescendants"/>