Scu*_*eve 47 android android-softkeyboard
我有一个带有一些EditText字段和一些按钮的Activity,以方便通常用于填充这些字段的内容.但是,当我们用户触摸其中一个EditText字段时,会自动显示Android软键盘.我希望它默认保持隐藏状态,除非用户长按菜单按钮.我已经找到了解决方案,并找到了几个答案,但到目前为止我无法让它们工作.
我尝试过以下方法:
1 - 在onCreate方法中,
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
2 - 同样在onCreate方法中,
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)
3 - 和fIn清单文件,
<activity android:name=".activityName" android:windowSoftInputMode="stateAlwaysHidden"/>
Run Code Online (Sandbox Code Playgroud)
这些方法都不起作用.只要用户单击EditText字段,就会出现软键盘.如果用户通过长按菜单键明确显示软键盘,我只想显示软键盘.
为什么这不起作用?
San*_*ndy 89
这会对你有所帮助
editText.setInputType(InputType.TYPE_NULL);
Run Code Online (Sandbox Code Playgroud)
编辑:
要显示软键盘,您必须编写以下代码 long key press event of menu button
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
Run Code Online (Sandbox Code Playgroud)
Emr*_*mza 31
您需要添加的下列属性Activity在你的AndroidManifest.xml.
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
/>
Run Code Online (Sandbox Code Playgroud)
我有时会使用一些技巧来做到这一点.我在布局的顶部放置了一个隐形焦点支架.就像这样
<EditText android:id="@id/editInvisibleFocusHolder"
style="@style/InvisibleFocusHolder"/>
Run Code Online (Sandbox Code Playgroud)
有这种风格
<style name="InvisibleFocusHolder">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:inputType">none</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后在onResume我会打电话
editInvisibleFocusHolder.setInputType(InputType.TYPE_NULL);
editInvisibleFocusHolder.requestFocus();
Run Code Online (Sandbox Code Playgroud)
这对我来说很有效,从1.6到4.x
经过长时间查看TextView类,我发现了一种防止键盘出现的方法.诀窍是它出现后立即隐藏它,所以我搜索了键盘出现后调用的方法并隐藏它.
实现了EditText类
public class NoImeEditText extends EditText {
public NoImeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* This method is called before keyboard appears when text is selected.
* So just hide the keyboard
* @return
*/
@Override
public boolean onCheckIsTextEditor() {
hideKeyboard();
return super.onCheckIsTextEditor();
}
/**
* This methdod is called when text selection is changed, so hide keyboard to prevent it to appear
* @param selStart
* @param selEnd
*/
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
hideKeyboard();
}
private void hideKeyboard(){
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
Run Code Online (Sandbox Code Playgroud)
和风格
<com.my.app.CustomViews.NoImeEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:background="@null"
android:textSize="@dimen/cell_text" />
Run Code Online (Sandbox Code Playgroud)
我的测试结果:
与setInputType:
editText.setInputType(InputType.TYPE_NULL);
Run Code Online (Sandbox Code Playgroud)
软键盘消失,但光标也会消失。
与setShowSoftInputOnFocus:
editText.setShowSoftInputOnFocus(false)
Run Code Online (Sandbox Code Playgroud)
它按预期工作。
即使我设置EditorInfo.TYPE_NULL了视图,软键盘仍然保持上升.除了我从nik431的答案得到的想法之外,没有一个答案对我有用:
editText.setCursorVisible(false);
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
Run Code Online (Sandbox Code Playgroud)
以下行正是正在寻找的内容。此方法已包含在 中API 21,因此适用于API 21及以上版本。
edittext.setShowSoftInputOnFocus(false);
Run Code Online (Sandbox Code Playgroud)