避免PopupWindow抓住焦点

时间:2012-04-27 07:24:23

标签: android popupwindow

我有一个EditText,可以在每个按键上更新PopupWindow。如果我使用.setFocusable(true)创建PopupWIndow,那么它会抓住焦点而我无法继续输入。但是,如果我使用.setFocusable(false),PopupWindow中的ListView不会触发OnItemClickedListener。

它是否可以创建一个PopupWindow而不会抓住焦点,但仍然可以让它内部的小部件可点击?

(PopupWidow不是自动完成的,所以我认为AutoCompleteTextView不是我需要的)

1 个答案:

答案 0 :(得分:2)

我猜你可以使用ListPopupWindow。它为您处理一些焦点问题。这是一个非常简单的代码。它仍然需要添加更多的听众以满足您的需求。但它确定了焦点问题。

ListPopupWindow popup;
EditText editText;
String[] strAry = {"a", "2", "3", "4", "5", "6", "7", "8", "9"};

editText=(EditText)this.findViewById(R.id.editText);
popup= new ListPopupWindow(this.getApplicationContext());
popup.setAdapter(new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,strAry));
popup.setAnchorView(editText);
popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);

editText.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View eventView) {
        if(!popup.isShowing()){
            popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);
            popup.show();
        }
    }
});
editText.addTextChangedListener(new s(){
    @Override
    public void afterTextChanged(Editable arg0) {}
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {}
    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        if(!popup.isShowing()){
            popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);
            popup.postShow();
        }
    }
});