我正在尝试在使用NumberPicker输入数值时停用软键盘(出于美观原因)。这是我的layout-xml代码:
<?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时,软键盘仍会显示。有任何想法吗?
刚发现这个,它就像一个魅力:
myNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
您也可以在XML中设置它:
android:descendantFocusability="blocksDescendants"
工作代码编程:
mp.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
XML:
android:descendantFocusability="blocksDescendants"
Xml版本的Andrew Webber的回答
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源代码阅读后,我得到了以下解决方案:
// 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(所以如果也投票给@MaxVogler这个投票)并使它成为一个强大的黑客。我们也不需要打电话给setOnFocusChangeListener
和setInputType
。只有setFocusable
到false才会这样做。
以下是启用/禁用该功能的帮助程序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
,在你的setOnTouchListener(this)
上调用NumberPicker
,并在你的onTouch(View v,MotionEvent e)
实现中将numberpicker后代的focusability重置为其正常值,然后返回false。
返回false意味着仍然由NumberPicker处理触摸,这意味着如果用户点击编辑框,则会出现软键盘。这恰好是我想要遇到的同样问题 - 当第一次显示时,软键盘出现对话框是令人不快的,因为它在出现后将对话框移开。
public class GetBufferDialog extends DialogFragment implements View.OnTouchListener {
在onCreateDialog()
方法中创建Dialog并找到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;
}
我发现最简单的工作是:
numberPicker = (NumberPicker) myDialogView.findViewById(R.id.myViewId);
EditText numberPickerChild = (EditText) numberPicker.getChildAt(0);
numberPickerChild.setFocusable(false);
numberPickerChild.setInputType(InputType.TYPE_NULL);
我不知道它为什么会起作用,但是设置OnClickListener什么都不会阻止键盘显示(Lollipop)
numberPicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
如果您只想在使用数字选择器加载视图时隐藏软件键盘,但仍希望用户能够在视图加载后进行编辑,那么您不应该阻止后代的可聚焦性。相反,只需阻止数字选择器成为视图中的第一个焦点项目。
有关详细信息,请参阅this 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"/>
/**
* 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();
}
}
}
*称这个: