弹出窗口

时间:2017-06-08 13:53:50

标签: android android-edittext android-popupwindow

我有一个popUp窗口,在活动中点击一个按钮就被调用,这个弹出窗口里面有一个edittext,我试图在输入EditText后点击输入调用搜索功能。但控件永远不会进入OnEditorActionListener我有还尝试了setOnKeyListener。

这是弹出窗口的代码:

private PopupWindow initiatePopupWindow(int i) {
try {
     mInflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = mInflater.inflate(R.layout.rowsearch, null);
View viewone = layout.findViewById(R.id.blankone);
View viewtwo = layout.findViewById(R.id.blanktwo);
searchedit = (EditText)layout.findViewById(R.id.search_edit);
//       searchedit.setImeActionLabel("Go", KeyEvent.KEYCODE_ENTER);
//       mDropdown.setFocusable(true);
//       mDropdown.update();
//       searchedit.setFocusableInTouchMode(true);
 searchedit.requestFocus();
 searchedit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        // Search function to be called here
                       Log.d("search","search");
                       searchfunc();

                        return true;
                    }
                    return false;
                }
            });
 layout.measure(View.MeasureSpec.UNSPECIFIED,
                    View.MeasureSpec.UNSPECIFIED);
            mDropdown = new PopupWindow(layout, FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT,true);

            mDropdown.showAsDropDown(linearLayout,5,5);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDropdown;

}

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<View
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:id="@+id/blankone">

</View>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="vertical"
    android:padding="20dp"
    android:background="@drawable/white_menu">


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="#b5b5b5"
            android:id="@+id/search_edit"
            android:imeOptions="actionDone"
            android:hint="what can we help you find?"/>

    </LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/blanktwo">

</View>

</LinearLayout>

我尝试过很多东西,比如setfocusable(true),但似乎没什么用,我想要searchfunc();在EditText中按下回车键调用。

1 个答案:

答案 0 :(得分:0)

android:inputType="text"添加到EditText解决了这个问题。

相关问题