0

我想在键盘输入和 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;
            }
        });

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

错误: KeyEvent 无法解析为类型

4

1 回答 1

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

IE

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;
于 2013-08-06T15:51:53.817 回答