setOnEditorActionListener不适用于android kitkat

时间:2016-05-18 09:30:07

标签: android android-studio

SetOnEditorListener无效。我有我的代码handleSearch()作为方法,我举杯但它不起作用。我无法找到错误。

private MenuItem mSearchAction;
private boolean isSearchOpened = false;
private EditText edtSeach;

 public boolean onOptionsItemSelected(MenuItem item) {
 int id = item.getItemId();
   if(id==R.id.action_search)
    {
        handleSearch();
    }}

这是我的handleSearch()方法。

private void handleSearch() {
    ActionBar action = getSupportActionBar();
    if(isSearchOpened){ //test if the search is open
        action.setDisplayShowCustomEnabled(false); 
        action.setDisplayShowTitleEnabled(true); 
        //hides the keyboard
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edtSeach.getWindowToken(), 0);

        mSearchAction.setIcon(getResources().getDrawable(R.drawable.search_image));
        isSearchOpened = false;
    } else { //open the search entry
        action.setDisplayShowCustomEnabled(true); 
        action.setCustomView(R.layout.search_bar);
        action.setDisplayShowTitleEnabled(false); 
        edtSeach =(EditText)action.getCustomView().findViewById(R.id.edtSearch);
        edtSeach.setSingleLine();
        edtSeach.setImeOptions(EditorInfo.IME_ACTION_GO);
         edtSeach.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_SHORT).show();
                Log.v("TAG", "onEditorAction(): " + actionId);
                if (actionId == EditorInfo.IME_ACTION_SEARCH ) {
                    Log.v("TAG", "onEditorAction(): entered");
                   webViewPlaceholder.loadUrl("http://www.google.com/search?q="+v.getText().toString());


                        return true;
                }
                return false;
            }
        });
        edtSeach.requestFocus();
        //open the keyboard focused in the edtSearch
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(edtSeach, InputMethodManager.SHOW_IMPLICIT);
        //add the close icon
        mSearchAction.setIcon(getResources().getDrawable(R.drawable.ic_close_search));
        isSearchOpened = true;
    }
}     

2 个答案:

答案 0 :(得分:0)

确保您的EditText包含 android:imeOptions =“actionSearch”

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtSearch"
android:imeOptions="actionSearch" />

只需在此处添加条件:

if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_GO) {
                Log.v("TAG", "onEditorAction(): entered");
               webViewPlaceholder.loadUrl("http://www.google.com/search?q="+v.getText().toString());


                    return true;
            }

答案 1 :(得分:0)

我希望它会对你有所帮助。

edtSeach.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.charAt(s.length() - 1) == '\n') {
                Log.d("SEARCH RESPONSE", "Enter was pressed");

        }
    })