Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AutoCompleteTextView始终保持焦点_Android_User Interface_Focus - Fatal编程技术网

Android AutoCompleteTextView始终保持焦点

Android AutoCompleteTextView始终保持焦点,android,user-interface,focus,Android,User Interface,Focus,我在一个活动(LinearLayout)中有两个自动完成文本视图和几个附加控件(放射组、按钮等)。不知何故,AutoCompleteTextView从未失去焦点 例如: 用户单击AutoCompleteTextView,控件将获得焦点。因此,光标开始闪烁,自动完成下拉列表和键盘显示。这很好。 但是,如果用户现在单击单选按钮中的一个(或另一个控件),则AutoCompleteTextView中的光标仍在闪烁,键盘仍在显示 如何使焦点自动消失 编辑:xml代码 <

我在一个活动(LinearLayout)中有两个自动完成文本视图和几个附加控件(放射组、按钮等)。不知何故,AutoCompleteTextView从未失去焦点

例如: 用户单击AutoCompleteTextView,控件将获得焦点。因此,光标开始闪烁,自动完成下拉列表和键盘显示。这很好。 但是,如果
用户现在单击单选按钮中的一个
(或另一个控件),则AutoCompleteTextView
中的光标仍在闪烁
,键盘仍在显示

如何使焦点自动消失

编辑: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="" />

您尝试过
android:focusableInTouchMode=“true”
每个视图的

代码片段

<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="" />

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

android:focusable="true" 
android:focusableInTouchMode="true"

对于AutoCompleteTextView(如LinearLayout等)的父级

为了避免将所有其他内容设置为可聚焦的
(如果您碰巧在许多其他布局中使用相同的文本视图,这会很痛苦),我们选择覆盖逻辑以在活动级别截获触摸屏事件:

@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);
}
@覆盖
公共布尔dispatchTouchEvent(MotionEvent){
视图v=getCurrentFocus();
if(v instanceof EditText){
int scrcoords[]=新int[2];
v、 getLocationOnScreen(scrcoords);
//计算单击位置相对于视图位置的相对位置
float x=event.getRawX()-scrcoords[0];
float y=event.getRawY()-scrcoords[1];
//检查操作是否已启动,并且单击位置是否在视图之外
如果(event.getAction()==MotionEvent.ACTION\u UP
&&(x<0 | | x>v.getRight()-v.getLeft()
||y<0 | | y>v.getBottom()-v.getTop()){
if(v.getOnFocusChangeListener()!=null){
v、 getOnFocusChangeListener().onFocusChange(v,false);
}
}
}
返回super.dispatchTouchEvent(事件);
}
如果您将此逻辑放在基本活动中,现在任何带有编辑文本的屏幕都将在您点击其外部的任何位置时触发
onFocusChange
。通过收听
onFocusChange
您可以在另一个视图上收听
clearFocus
requestFocus
。这或多或少是一个技巧,但至少你不必为许多版面上的任何其他项目设置焦点


请参见

向我们展示一些相应自动完成的XML代码您好,我在上面添加了XML代码否,不幸的是这并没有改变任何事情。请参见@Michal Heneš您的解决方案与@Michal Heneš的答案相结合,帮助我解决了我的问题。谢谢。