Android - 对话框片段:始终隐藏虚拟键盘
Android - Dialog fragment: always hide virtual keyboard
我有一个自定义对话框,它是一个 DialogFragment。这个对话框有一个 EditText 和我自己的键盘视图,所以我不想使用默认的虚拟键盘。
每次用户触摸 EditText 时,我都会隐藏虚拟键盘:
edtAmount.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
View view = this.getDialog().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
return true;
}
});
但是因为系统还是调用了虚拟键盘显示(之前是强行隐藏),所以系统上下移动我的dialog很快。这样不好。
谁能帮我避免像这样弹出对话框,让它保持不动?
PS:我在清单中试过:
android:windowSoftInputMode="adjustNothing"
不过好像不行。
非常感谢。
编辑
我想保留光标,所以我在这个线程中找到了解决方案:
希望这对某些人有所帮助。
试试这个代码。在我的应用程序中它完美运行
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
这只能在您从自定义 EditText 扩展 EditText 时完成,请使用以下代码用于自定义 EditText,它永远不会打开软键盘...!
public class DisableSoftKeyBoardEditText extends EditText {
public DisableSoftKeyBoardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}
我有一个自定义对话框,它是一个 DialogFragment。这个对话框有一个 EditText 和我自己的键盘视图,所以我不想使用默认的虚拟键盘。 每次用户触摸 EditText 时,我都会隐藏虚拟键盘:
edtAmount.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
View view = this.getDialog().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
return true;
}
});
但是因为系统还是调用了虚拟键盘显示(之前是强行隐藏),所以系统上下移动我的dialog很快。这样不好。
谁能帮我避免像这样弹出对话框,让它保持不动?
PS:我在清单中试过:
android:windowSoftInputMode="adjustNothing"
不过好像不行。
非常感谢。
编辑 我想保留光标,所以我在这个线程中找到了解决方案:
希望这对某些人有所帮助。
试试这个代码。在我的应用程序中它完美运行
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
这只能在您从自定义 EditText 扩展 EditText 时完成,请使用以下代码用于自定义 EditText,它永远不会打开软键盘...!
public class DisableSoftKeyBoardEditText extends EditText {
public DisableSoftKeyBoardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}