Android:问题使用键盘输入按钮说“搜索”并处理其点击?

时间:2013-08-06 15:45:03

标签: java android search-engine textedit

我想在键盘输入和EditText上添加一个搜索按钮,我按照以下步骤进行搜索,我发现了以下错误

recherche=(EditText)findViewById(R.id.recherche);

recherche.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                    new DownloadTask().execute();                   
                    return true;
                }
                return false;
            }
        });

错误:新类型TextView.OnEditorActionListener(){}必须实现继承的抽象方法  TextView.OnEditorActionListener.onEditorAction(TextView,int,KeyEvent)

错误:无法将KeyEvent解析为类型

1 个答案:

答案 0 :(得分:0)

以下是我的代码示例:

TextView.OnEditorActionListener enterListener = new TextView.OnEditorActionListener() {
   @Override
   public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
       if ((keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (i == EditorInfo.IME_ACTION_DONE)) {
            InputMethodManager inputManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                   InputMethodManager.HIDE_NOT_ALWAYS);
            bQServer.performClick();
        }
        return false;
    }
};

//etUserName.setOnEditorActionListener(enterListener);
etPassword.setOnEditorActionListener(enterListener);

您应该只需更改KeyEvent.KEYCODE_ENTER

KeyEvent.KEYCODE_SEARCH即可

TextView.OnEditorActionListener enterListener = new TextView.OnEditorActionListener() {
       @Override
       public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
           if ((keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_SEARCH)) {
               new DownloadTask().execute();
               return true; 
            }
            return false;
        }
    };

recherche.setOnEditorActionListener(enterListener);

- 编辑 -

确保已导入KeyEvent和onEditorActionListener

import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;