我有一个活动中的2 AutoCompleteTextViews
(LinearLayout)和几个additional controls
(无线电组、按钮等)。不知何故,AutoCompleteTextViews是never losing focus
。
例如:用户单击一个AutoCompleteTextView,该控件将获得焦点。因此光标开始闪烁,自动完成下拉列表和键盘显示。这很好。但是,如果user now clicks on of the radio buttons
(或其他控件),则仍会显示AutoCompleteTextView is still blinking
和键盘中的光标。
如何使焦点自动消失?
编辑: xml代码
<AutoCompleteTextView
android:id="@+id/ediFrom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="" />
发布于 2014-04-26 08:43:38
唯一对我有用的解决方案是添加这一行。
android:focusable="true"
android:focusableInTouchMode="true"
给AutoCompleteTextView的父级(如LinearLayout等)
发布于 2014-02-21 09:36:58
您尝试过使用android:focusableInTouchMode="true"
获取each view
代码片段吗?
<AutoCompleteTextView
android:id="@+id/ediFrom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:focusableInTouchMode="true"
android:text="" />
http://android-developers.blogspot.in/2008/12/touch-mode.html
发布于 2014-11-27 20:29:42
为了避免设置所有其他focusable
(如果您碰巧在许多其他布局中使用相同的文本视图,这将是痛苦的),我们选择重写逻辑来在活动级别拦截触摸屏事件:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
if (v instanceof EditText) {
int scrcoords[] = new int[2];
v.getLocationOnScreen(scrcoords);
// calculate the relative position of the clicking position against the position of the view
float x = event.getRawX() - scrcoords[0];
float y = event.getRawY() - scrcoords[1];
// check whether action is up and the clicking position is outside of the view
if (event.getAction() == MotionEvent.ACTION_UP
&& (x < 0 || x > v.getRight() - v.getLeft()
|| y < 0 || y > v.getBottom() - v.getTop())) {
if (v.getOnFocusChangeListener() != null) {
v.getOnFocusChangeListener().onFocusChange(v, false);
}
}
}
return super.dispatchTouchEvent(event);
}
如果你把这个逻辑放在你的基本活动中,当你点击它之外的任何地方时,任何有编辑文本的屏幕都会触发onFocusChange
。通过收听onFocusChange
,您可以在另一个视图上使用clearFocus
或requestFocus
。这或多或少是一次黑客攻击,但至少你不需要在许多布局上为任何其他项目设置可调的焦点。
https://stackoverflow.com/questions/21939657
复制相似问题