如何处理输入按下的键android

时间:2014-09-26 08:57:37

标签: android search keyboard android-edittext swipe

我正在开发一个在线商店的应用程序。其中一个主要功能是搜索功能。由于我需要支持API 8及更高版本而不是SearchView,我使用了下面的EditText来实现搜索视图。

                <EditText android:id="@+id/search_edit"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_toLeftOf="@+id/search_button"
                        android:background="@color/white"
                        android:ems="20"
                        android:imeOptions="actionDone"
                        android:maxLines="1"
                        android:textColor="@color/black"
                        android:textColorHint="@color/ebuy_color_second_strip"
                        android:textSize="18sp" >
                    </EditText>

现在我想处理键盘的回车键,其方式与用户按回车键相比,搜索活动开始。我使用的代码如下:

search = (EditText) findViewById(R.id.search_edit);
        search.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                    String query_text = search.getText().toString().trim();

                    try {
                        query_text = URLEncoder.encode(query_text, "utf-8");
                    } catch (UnsupportedEncodingException e) {

                    }
                    String full_query = query_text;

                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(search.getWindowToken(), 0);

                        Intent bussiness = new Intent(EbuyHomeScreen.this,
                                EbuySearchActivity.class);
                        Bundle basket_buss_category = new Bundle();

                        basket_buss_category.putString(EbuyUtils.TAG_CATEGORY_CODE, full_query);

                        bussiness.putExtras(basket_buss_category);

                        startActivity(bussiness);

                    return true;
                }
                return false;
            }

        });

这种方式与Android键盘和其他一些键盘完美配合,但是当我使用从Google Play商店下载的滑动或快速键盘和其他个性化键盘时,这不起作用,而不是开始活动跳转到新队。有人可以帮我解决这类问题。

1 个答案:

答案 0 :(得分:0)

在您的edittext上尝试 setOnEditorActionListener

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
        //your code here
        return true;
    }
    return false;
}
});

您需要在edittext的xml文件中设置 imeAction

<EditText
    android:id="@+id/editTextUsername"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="USERNAME"
    android:inputType="text"
    android:imeOptions="actionSearch" >

    <requestFocus />
</EditText>