致所有人:
我正在尝试让一个简单的NumberPicker在AlertDialog中工作。问题是,每当我增加/减少数字选择器中的值时,键盘就会激活。
有很多帖子描述了这个问题,但没有一个建议有效。我试过了:
android:configChanges="keyboard|keyboardHidden"
和
inputManager.hideSoftInputFromWindow(currentView.getWindowToken(), 0);
和
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
我尝试过在初始化之前和之后调用这些函数(dialog.show ()),在按键事件上(显然是使用侦听器),等等,但到目前为止还没有成功。
完整的代码:
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myNumber"
android:configChanges="keyboard|keyboardHidden"
/>
</RelativeLayout>
和调用函数:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
View view = getLayoutInflater().inflate(R.layout.popup, null);
builder.setView (view);
final AlertDialog dialog = builder.create ();
NumberPicker picker = (NumberPicker) view.findViewById(R.id.myNumber);
picker.setMinValue(0);
picker.setMaxValue(999);
dialog.getWindow().
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
dialog.show();
感谢您的任何帮助
巴里
发布于 2012-06-13 01:56:17
实际上,尽管上面的解决方案工作得很好,但还有一种更简单的方法。
picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
这就是所需要的一切。无论如何,感谢您的回复!:-)
发布于 2012-06-12 22:50:13
因为无法访问NumberPicker按钮,所以“不可能”这样做。
我做了一个又脏又脏的黑客来处理它。
首先向布局添加焦点食客,在本例中为0大小的Button:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myNumber"
android:configChanges="keyboard|keyboardHidden"
/>
<Button
android:id="@+id/myBtn"
android:layout_width="0dp"
android:layout_height="0dp"
/>
</RelativeLayout>
我们需要捕捉点击递增/递减按钮的事件,我选择了OnValueChangedListener,找不到更好的东西。
EditText edit = null;
try {
final Field[] fields = picker.getClass().getDeclaredFields();
for (Field f : fields) {
if (EditText.class.equals(f.getType())) {
f.setAccessible(true);
edit = (EditText) f.get(picker);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
if (edit != null) {
final EditText finalEdit = edit;
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
dialog.findViewById(R.id.myBtn).requestFocusFromTouch();
imm.hideSoftInputFromWindow(finalEdit.getWindowToken(), 0);
}
});
}
这不是推荐的解决方案。我希望有人能找到更好的东西。只作教育用途;-)
发布于 2012-06-12 21:00:16
android:configChanges
属性属于您的Manifest.xml
文件,而不是布局。但是如果你把键盘藏在那里,你最好使用
android:windowSoftInputMode="stateAlwaysHidden"
您还应该尝试在Activity
上使用getWindow()
,而不仅仅是在Dialog
上,看看这是否有帮助。这可能是最好的解决方案,因为第一个解决方案(隐藏清单)将在整个活动中隐藏键盘。
https://stackoverflow.com/questions/10996880
复制相似问题