安卓AutoCompleteTextView作为ListView的头 [英] Android AutoCompleteTextView as ListView header

查看:199
本文介绍了安卓AutoCompleteTextView作为ListView的头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个AutoCompleteTextView作为一个ListView头,但如果我这样做自动完成框永远不会出现。在$ C $下创建自动完整视图直接来自的你好,自动完成教程在谷歌文档。国阵也来自那里。

I'm trying to set an AutoCompleteTextView as a ListView header, but if I do so the autocomplete box never appears. The code for creating the auto complete view comes directly from the Hello, AutoComplete tutorial in googles docs. The COUNTRIES array also comes from there.

protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView myList = (ListView) findViewById(R.id.ResultList);

LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
TableLayout searchHeader = (TableLayout) layoutInflater.inflate(R.layout.search_header, null); 
myList.addHeaderView(searchHeader, null, false);

final AutoCompleteTextView textView = (AutoCompleteTextView) myList.findViewById(R.id.edit);
ArrayAdapter<String> searchAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(searchAdapter);
textView.setThreshold(1);

        //Dummy data for listview.

String[] listContent = {
        "test", "test", "test", "test",
        "test", "test", "test", "test"
};
ArrayAdapter<String> adapter = new SearchResultAdapter(this, listContent);
myList.setAdapter(adapter);

}

作为测试,我添加了一个TextChangedListener尝试和力显示对话框

As a test, I added a TextChangedListener to try and force show the dialog

    textView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            textView.showDropDown();
        }
    });

出现对话框,但几乎立即关闭。我不知道如果某种事件的列表视图冒泡是导致此?

The dialog appears but is closed almost instantly. I wonder if some kind of event bubbling from the list view is causing this?

推荐答案

这个问题有关的一个ListView和功放内的EditText意见重点;在AutoCompleteTextView源的一些阅读帮助我找到答案。设置焦点的顺序ListView控件来afterDescendants允许对话框正常显示。

This question regarding focus of EditText views within a ListView & some reading of the AutoCompleteTextView source helped me find the answer. Setting the order of focus on the ListView to afterDescendants allowed the dialog to be shown normally.

<ListView
    android:id="@+id/ResultList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="50dip"
    android:descendantFocusability="afterDescendants"
    android:fadingEdge="none" >
</ListView>

这篇关于安卓AutoCompleteTextView作为ListView的头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆