首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >AutoCompleteTextView总是保持焦点

AutoCompleteTextView总是保持焦点
EN

Stack Overflow用户
提问于 2014-02-21 08:21:40
回答 3查看 10.7K关注 0票数 6

我有一个活动中的2 AutoCompleteTextViews (LinearLayout)和几个additional controls (无线电组、按钮等)。不知何故,AutoCompleteTextViews是never losing focus

例如:用户单击一个AutoCompleteTextView,该控件将获得焦点。因此光标开始闪烁,自动完成下拉列表和键盘显示。这很好。但是,如果user now clicks on of the radio buttons (或其他控件),则仍会显示AutoCompleteTextView is still blinking和键盘中的光标。

如何使焦点自动消失?

编辑: xml代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
                <AutoCompleteTextView
                android:id="@+id/ediFrom"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:singleLine="true"
                android:text="" />
EN

回答 3

Stack Overflow用户

发布于 2014-04-26 08:43:38

唯一对我有用的解决方案是添加这一行。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
android:focusable="true" 
android:focusableInTouchMode="true"

给AutoCompleteTextView的父级(如LinearLayout等)

票数 8
EN

Stack Overflow用户

发布于 2014-02-21 09:36:58

您尝试过使用android:focusableInTouchMode="true"获取each view代码片段吗?

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<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

票数 2
EN

Stack Overflow用户

发布于 2014-11-27 20:29:42

为了避免设置所有其他focusable (如果您碰巧在许多其他布局中使用相同的文本视图,这将是痛苦的),我们选择重写逻辑来在活动级别拦截触摸屏事件:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@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,您可以在另一个视图上使用clearFocusrequestFocus。这或多或少是一次黑客攻击,但至少你不需要在许多布局上为任何其他项目设置可调的焦点。

请参阅http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21939657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文