editText 没有失去焦点
当我单击它时,editText 不会失去我的应用程序中的焦点,它始终具有橙色边框和黑线光标。 我根据 editText 周围的情况做了一个 LinearLayout: 阻止 EditText 在 Activity 启动时获得焦点 所以它不会集中在应用程序的启动上。
这是我的代码:
final EditText et = (EditText)findViewById(R.id.EditText01);
final InputMethodManager imm = (InputMethodManager) getSystemService(
INPUT_METHOD_SERVICE);
et.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}
});
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasfocus) {
if(hasfocus) {
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
} else {
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
}
});
但是,当我单击 editText 之外的任何地方时,它似乎没有调用 onFocusChange!
the editText is not losing focus in my app when I click out of it, it has the orange border all time and that black line cursor..
I did this a LinearLayout according to this surrounding the editText:
Stop EditText from gaining focus at Activity startup
so it doesn't get focus on start of the app..
this is my code:
final EditText et = (EditText)findViewById(R.id.EditText01);
final InputMethodManager imm = (InputMethodManager) getSystemService(
INPUT_METHOD_SERVICE);
et.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}
});
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasfocus) {
if(hasfocus) {
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
} else {
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
}
});
But, it doesn't seem to be calling the onFocusChange when I click anywhere outside the editText!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很简单。您可以将以下内容放入 LinearLayout 或任何其他布局中:
It's simple. You can put the following in LinearLayout or any other else:
覆盖
Activity.dispatchTouchEvent()
:这样,每当有人单击任何位置时,您都可以进行控制,并且仅在代码中的一个点进行控制。
Override
Activity.dispatchTouchEvent()
:That way, whenever someone clicks anywhere, you have control, and it's at only one point in code.
创建一个
onClick
侦听器(可能在 XML 布局中)以侦听后台 LinearLayout 中的点击。在将EditText
设置为您正在使用的 Activity 的实例变量后,您可以从这里调用.clearFocus()
。Create an
onClick
listener, possibly in your XML layout to listen for clicks in the background LinearLayout. From here you can call.clearFocus()
on yourEditText
after you make it an instance variable of the activity you are working in.您可以通过调用该方法将焦点设置到其他字段来从 EditText 中删除焦点。
假设我已将焦点设置为单击完成按钮时的后退按钮,然后在完成按钮单击侦听器内调用后退按钮的方法。
你的问题已经解决了。
You can remove the focus from the EditText, by setting the focus to other field by calling the method.
Suppose I have set the focus to back button on click of done button, then call the method on back button inside done button click listener.
And your problem is solved.