我在一个活动中有三个EditText框,其中两个正常输入方法(硬键,默认软键盘)都可以.但是对于其中一个EditText框,我只想从自定义键盘视图发送软输入.所以实际上我希望永远不会为这个EditText显示默认的软键盘.我尝试为EditText添加onTouchListeners和onFocusChange侦听器,部分成功如下:
public boolean onTouch(View v, MotionEvent event) {
v.requestFocus();
imm.toggleSoftInput(0, 0);
return true;
}
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive(v)) {
imm.toggleSoftInput(0,0);
}
}
Run Code Online (Sandbox Code Playgroud)
但我还没有找到明确的解决方案,因为
1)默认软键盘在听众隐藏之前总是短暂闪烁
2)在某些情况下,例如使用硬键盘箭头键将焦点移动到EditText有时会将默认软键盘设置为可见
等等.
所以我很想找到一种简单的方法来告诉Android永远不要为这个特定的EditText显示默认的软键盘.我不想扩展EditText并开始覆盖东西,因为EditText功能对我来说是完美的 - 我只是希望不显示默认的软键盘.
我花了几天时间试图解决这个问题.通过谷歌找到的一些主题(包括一些在这里)有一半尝试这个问题,但到目前为止,我还没有找到一个完全功能的解决方案.
编辑:
我真的开始生气了.我决定不尝试使用EditText,而是使用其他任何可以完成工作的视图.事实证明,摆脱那个软键盘是很难的.当我使用硬键将焦点从EditText移动到Button时,它甚至出现!为什么软键盘应该显示在碰巧有焦点的每个怪物视图上?即使我明确说inputType ="none"?如何关闭*软键盘?下面是Button的xml - 让我们以此为例:
<Button
android:id="@+id/OkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="none"
android:paddingRight="5mm"
android:paddingLeft="5mm"
android:layout_below="@id/Volume"
android:layout_alignParentLeft="true"
android:text="OK"/>
Run Code Online (Sandbox Code Playgroud)
EDIT2:
我是如何实现一个似乎有效的解决方案.首先,我得到了InputMethodManager:
this.imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
Run Code Online (Sandbox Code Playgroud)
我设置OnClickListener,OnTouchListener和OnFocusChange监听器,当我想要聚焦EditText并且我的自定义KeyboardView可见时,所有人都调用以下方法,同时隐藏默认的软输入:
private boolean makeActive(View v) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
EditText e = (EditText) v;
int iType = e.getInputType();
e.setInputType(InputType.TYPE_NULL);
e.requestFocus();
showKb();
e.setInputType(iType);
return true;
}
Run Code Online (Sandbox Code Playgroud)
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(searchBox.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
其中 SearchBox 是您的文本框或更好,而不是 SearchBox 获取您当前显示的窗口。
或者尝试:
InputMethodManager imm = (InputMethodManager)getBaseContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)
上下文在哪里getApplicationContext();
| 归档时间: |
|
| 查看次数: |
3291 次 |
| 最近记录: |