EditText, clear focus on touch outside(EditText,清晰专注于触摸外)
问题描述
我的布局包含 ListView
、SurfaceView
和 EditText
.当我单击 EditText
时,它会获得焦点并弹出屏幕键盘.当我单击 EditText
之外的某个位置时,它仍然具有焦点(它不应该).我想我可以在布局中的其他视图上设置 OnTouchListener
并手动清除 EditText
的焦点.但似乎太 hackish...
My layout contains ListView
, SurfaceView
and EditText
. When I click on the EditText
, it receives focus and the on-screen keyboard pops up. When I click somewhere outside of the EditText
, it still has the focus (it shouldn't).
I guess I could set up OnTouchListener
's on the other views in layout and manually clear the EditText
's focus. But seems too hackish...
我在其他布局中也有相同的情况 - 具有不同类型项目的列表视图,其中一些具有 EditText
的内部.他们的行为就像我上面写的一样.
I also have the same situation in the other layout - list view with different types of items, some of which have EditText
's inside. They act just like I wrote above.
任务是让 EditText
在用户触摸外部的东西时失去焦点.
The task is to make EditText
lose focus when user touches something outside of it.
我在这里看到过类似的问题,但没有找到任何解决方案...
I've seen similar questions here, but haven't found any solution...
推荐答案
我尝试了所有这些解决方案.edc598 最接近工作状态,但触摸事件并未在布局中包含的其他 View
上触发.如果有人需要这种行为,我最终会这样做:
I tried all these solutions. edc598's was the closest to working, but touch events did not trigger on other View
s contained in the layout. In case anyone needs this behavior, this is what I ended up doing:
我创建了一个名为 touchInterceptor 的(不可见的)FrameLayout
作为布局中的最后一个 View
,以便它覆盖所有内容(edit: 您还必须使用 RelativeLayout
作为父布局并赋予 touchInterceptor fill_parent
属性).然后我用它来拦截触摸并确定触摸是否在 EditText
之上:
I created an (invisible) FrameLayout
called touchInterceptor as the last View
in the layout so that it overlays everything (edit: you also have to use a RelativeLayout
as the parent layout and give the touchInterceptor fill_parent
attributes). Then I used it to intercept touches and determine if the touch was on top of the EditText
or not:
FrameLayout touchInterceptor = (FrameLayout)findViewById(R.id.touchInterceptor);
touchInterceptor.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (mEditText.isFocused()) {
Rect outRect = new Rect();
mEditText.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
mEditText.clearFocus();
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return false;
}
});
返回 false 让触摸处理失败.
Return false to let the touch handling fall through.
这很hacky,但它是唯一对我有用的东西.
It's hacky, but it's the only thing that worked for me.
这篇关于EditText,清晰专注于触摸外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:EditText,清晰专注于触摸外
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01