打开弹出窗口后,继续关注EditText?

时间:2015-02-04 20:55:35

标签: android android-edittext focus

我不确定这是否可行,但我有一个EditText,我将其用作搜索框。当用户触摸该框时,将创建ListPopUpWindow窗口小部件并显示搜索结果列表,并在用户键入时对其进行过滤。问题是当列表弹出时它会从编辑文本中删除控制权,因此在键入一个字母后,它们将失去控制,直到弹出窗口关闭。

我已经尝试过立即在EditText上调用requestfocus(),但它不起作用。这是我的代码:

public class SearchablePopupList {

EditText anchorView;    //The view that the pop up list will be attached to
Activity activity;
private ListPopupWindow listPopupWindow;
private ArrayList<String> filterList;

public SearchablePopupList(final EditText anchorView, Activity activity, final ArrayList<String> filterList){

    this.anchorView = anchorView;
    this.activity = activity;
    this.filterList = filterList;


     listPopupWindow = new ListPopupWindow(activity);

      ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(activity, 
               android.R.layout.simple_expandable_list_item_1, filterList);

     listPopupWindow.setAdapter(mAdapter);
            listPopupWindow.setAnchorView(anchorView);


            listPopupWindow.setHeight(600);
            listPopupWindow.setModal(true);
            listPopupWindow.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                     anchorView.setText(filterList.get(position));
                        listPopupWindow.dismiss();

                }});


            anchorView.addTextChangedListener(new TextWatcher(){

                @Override
                public void beforeTextChanged(CharSequence s, int start,
                        int count, int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence s, int start,
                        int before, int count) {
                    listPopupWindow.show();
                    anchorView.requestFocus();

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub

                }});                

}

}

0 个答案:

没有答案