Android setOnKeyListener在片段中使用时不工作

Android setOnKeyListener在片段中使用时不工作,android,android-fragments,android-edittext,android-keypad,Android,Android Fragments,Android Edittext,Android Keypad,在过去,我已经能够成功地设置onKeyListener,它将检测用户是否选择了android键盘上的返回按钮,然后启动并执行操作以将用户发送到另一个活动,但是我现在需要将相同的功能构建成一个片段,但是,当我在片段中使用setOnKeyListener时,由于某些原因它不起作用,下面我将发布代码 我的编辑文本 <EditText android:id="@+id/searchetedittext" android:layout_

在过去,我已经能够成功地设置onKeyListener,它将检测用户是否选择了android键盘上的返回按钮,然后启动并执行操作以将用户发送到另一个活动,但是我现在需要将相同的功能构建成一个片段,但是,当我在片段中使用setOnKeyListener时,由于某些原因它不起作用,下面我将发布代码

我的编辑文本

<EditText
                android:id="@+id/searchetedittext"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:background="@drawable/vineline_edittext_inner_background"
                android:gravity="top"
                android:hint="Search #Hashtags"
                android:imeOptions="actionSend"
                android:inputType="textCapSentences|textMultiLine|textAutoCorrect|textAutoComplete"
                android:lines="1"
                android:padding="5dp"
                android:textColor="@color/button_color_dark"
                android:textSize="22dp" />

这是一个老问题,但我在设置edittext的输入类型时得到了答案。android:inputType=“textPersonName”

您能告诉我们在哪里设置此侦听器吗?我的意思是,在片段类的onCreateView中,您在哪个回调中检查了onKey()方法是否正在被调用?也许searchFriendButton.performClick()是一个根本没有被调用的问题,我必须试着找出为什么这不会发生在片段中。这个答案很古老,但它帮助了我。我的EditText视图中缺少输入类型。尝试将onKeyListener置于对话框片段中并编辑文本视图。谢谢你!我添加了android:inputType=“textcaptensions”,它立即起作用,所以输入类型似乎并不重要,只要您指定一种。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View v;

    if (getArguments().getInt(EXTRA_SEARCH_TYPE) == 0) {
        v = inflater.inflate(R.layout.content_of_tab1, container, false);
        lv = (ListView) v.findViewById(R.id.searchtrendlist);
        pb = (ProgressBar) v.findViewById(R.id.progressbartab1);

        final LinearLayout searchButton = (LinearLayout) v
                .findViewById(R.id.searchbutton);

        final EditText searchEdittext = (EditText) v
                .findViewById(R.id.searchetedittext);
        searchEdittext.setTypeface(textStyleThin);

        searchEdittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    switch (keyCode) {

                    case KeyEvent.KEYCODE_ENTER:
                        searchButton.performClick();
                        return true;
                    default:
                        break;
                    }

                }
                return false;
            }
        });

        searchButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                postText = searchEdittext.getText().toString();

                Intent intent = new Intent(context,
                        HashtagScreenActivity.class);
                intent.putExtra("screenname", postText);
                startActivity(intent);

                searchEdittext.setText("");
            }
        });

        new setupTrends().execute();

    } else {
        v = inflater.inflate(R.layout.content_of_tab2, container, false);
        lv = (ListView) v.findViewById(R.id.searchfriendlist);
        pb = (ProgressBar) v.findViewById(R.id.progressbartab2);

        final LinearLayout searchFriendButton = (LinearLayout) v
                .findViewById(R.id.searchfriendbutton);

        final EditText searchFriendEdittext = (EditText) v
                .findViewById(R.id.searchfriendedittext);
        searchFriendEdittext.setTypeface(textStyleThin);

        searchFriendEdittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    switch (keyCode) {

                    case KeyEvent.KEYCODE_ENTER:
                        searchFriendButton.performClick();
                        return true;
                    default:
                        break;
                    }

                }
                return false;
            }
        });

        searchFriendEdittext.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable text) {
            }

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

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

                try {
                    fdb.open();

                    if (s.toString().matches("")) {
                        friendListItems = fdb.getFriends();
                    } else {
                        friendListItems = fdb.sortFriends(s.toString());
                    }

                    userListsAdapter = new SearchFriendsAdapter(
                            getActivity(), friendListItems);

                    lv.setAdapter(userListsAdapter);

                    fdb.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                searchFriendEdittext.requestFocus();

            }
        });

        searchFriendButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                postFriendText = searchFriendEdittext.getText().toString();

                if (postFriendText.length() > 0) {
                    Intent intent = new Intent(getActivity(),
                            FriendProfileActivity.class);
                    intent.putExtra("screenname", postFriendText);
                    startActivity(intent);

                    searchFriendEdittext.setText("");
                } else {
                    Toast.makeText(getActivity(),
                            "Please enter a twitter username",
                            Toast.LENGTH_LONG).show();
                }

            }
        });

        fdb.open();

        friendListItems = fdb.getFriends();

        userListsAdapter = new SearchFriendsAdapter(getActivity(),
                friendListItems);

        lv.setAdapter(userListsAdapter);

        fdb.close();

        pb.setVisibility(View.GONE);
    }

    addHeader();

    return v;
}