在NumberPicker上禁用软键盘

我试图在使用NumberPickerinput数字值(出于美学原因)时closures软键盘。 这是我的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> 

最后,这是我尝试在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时,软键盘仍然显示出来。 有任何想法吗?

只要find它,它就像一个魅力:

myNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

你也可以用XML来设置它:

 android:descendantFocusability="blocksDescendants" 

安德鲁·韦伯的答案的Xml版本

 android:descendantFocusability="blocksDescendants" 

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

阅读完com / android / internal / widget / NumberPicker.java源代码后,我得到了以下解决scheme:

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

只是增强了@MaxVogler的ans(所以如果这个投票也是这么投票的话),并且使其成为一个强大的黑客。 我们setOnFocusChangeListener需要调用setOnFocusChangeListenersetInputType 。 只有setFocusable为false才能做到。

以下是启用/禁用该function的助手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; } } } 

这里有另外一种方法可以让用户仍然可以编辑一个数字,如果他们愿意的话 – 最初只是压缩软键盘。 使用NumberPicker.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS)来抑制软键盘,当按照上面的答案界面第一次显示时。 然后让你的对话框或者活动实现View.OnTouchListener ,在你的onTouch(View v,MotionEvent e)上调用setOnTouchListener(this) ,在你的onTouch(View v,MotionEvent e) MotionEvent onTouch(View v,MotionEvent e)实现中onTouch(View v,MotionEvent e)将numberpicker后代的聚焦能力重置为正常值,然后返回false。

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

 public class GetBufferDialog extends DialogFragment implements View.OnTouchListener { 

onCreateDialog()方法中创build对话框并findNumberPicker之后:

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

我发现最简单的工作是:

 numberPicker = (NumberPicker) myDialogView.findViewById(R.id.myViewId); EditText numberPickerChild = (EditText) numberPicker.getChildAt(0); numberPickerChild.setFocusable(false); numberPickerChild.setInputType(InputType.TYPE_NULL); 
 /** * 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将是可编辑的)

  • 会在创build时隐藏软input

  • 之前(处理input)getValue()将允许得到适当的walue