[英]Detect EditText pressed outside of custom keyboard (IME) Android
I am developing an android keyboard with a search box inside it.我正在开发一个带有搜索框的 android 键盘。 I can detect when the user press my search box and then commit the text he types to my search box.
我可以检测到用户何时按下我的搜索框,然后将他键入的文本提交到我的搜索框中。
The problem is that I can't manage to detect when the user is pressing the original EditText (the one that opened the keyboard) because it's not my view.问题是我无法检测到用户何时按下原始 EditText(打开键盘的那个),因为这不是我的观点。
This is how basically the keyboard looks like:这是键盘的基本外观:
The keyboard is surrounded by the blue square, and my edit text is surrounded by the red square.键盘被蓝色方块包围,我的编辑文本被红色方块包围。
I tried setting this listener on my editText but with no luck: ( source )我尝试在我的 editText 上设置这个监听器,但没有运气:( 来源)
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
Toast.makeText(getApplicationContext(), "Got the focus", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
}
}
});
The listener isn't being called when I press on the other editText.当我按下另一个 editText 时,没有调用侦听器。 Probably because it is not a part of my service.
可能是因为它不是我的服务的一部分。
Even if I could some how detect a click on screen that is outside of my service layout, it would be great.即使我可以知道如何检测在我的服务布局之外的屏幕上的点击,它也会很棒。
Thanks for any help!谢谢你的帮助!
Although your question is very basic I think I can help here try this below snip of code and let me know if it works for you.尽管您的问题非常基本,但我想我可以在这里提供帮助,请尝试以下代码片段,并告诉我它是否适合您。
public void showKeyboard(View view) {
InputMethodManager myKeyboard = (InputMethodManager) getSystemService(Context
.INPUT_METHOD_SERVICE);
myKeyboard.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public void hideKeyboard() {
InputMethodManager myKeyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
myKeyboard.hideSoftInputFromWindow(EditText1.getWindowToken(), 0);
}
Also this is a duplicate question, see here: Android: show soft keyboard automatically when focus is on an EditText这也是一个重复的问题,请参见此处: Android:当焦点位于 EditText 时自动显示软键盘
Please try the ol' Google before posting questions here, it can stall your progress greatly while you wait for others to comment on your post when the answer is out there :) Also as a side note if you post duplicate questions and do not get enough up votes on your post, Stack Overflow can ban you from posting.请在此处发布问题之前尝试使用 ol' 谷歌,当您等待其他人对您的帖子发表评论时,它会大大拖延您的进度:) 如果您发布重复的问题并且没有得到足够的答案,也可以作为旁注对您的帖子进行投票,Stack Overflow 可以禁止您发帖。
Also you can review the Android Developer Docs on this topic as well: https://developer.android.com/training/keyboard-input/visibility您也可以查看有关此主题的 Android 开发人员文档: https : //developer.android.com/training/keyboard-input/visibility
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.