美文网首页
软键盘实现搜索功能是跳转activity打开两次

软键盘实现搜索功能是跳转activity打开两次

作者: Victory_IT | 来源:发表于2017-03-13 23:29 被阅读0次

    关键在于:,解决方法是调用setOnEditorActionListener而不是用setOnKeyListener来监听点击搜索按钮。

    第一种写法:

     circleEtSousuo.setOnKeyListener(new View.OnKeyListener() {

    @Override

    public boolean onKey(View view, int i, KeyEvent keyEvent) {

    if (i == KeyEvent.KEYCODE_ENTER) {

    //先隐藏键盘

    ((InputMethodManager) SearchActivity.this.getSystemService(INPUT_METHOD_SERVICE))

    .hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()

    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    //进行搜索操作的方法,在该方法中可以加入mEditSearchUser的非空判断

    intent.setClass(SearchActivity.this, MyCollectActivity.class);

    startActivity(intent);

    }

    return false;

    }

    });*/

    修正以后解决问题:

    circleEtSousuo.setOnEditorActionListener(newTextView.OnEditorActionListener() {

    @Override

    public booleanonEditorAction(TextView textView,inti, KeyEvent keyEvent) {

    if(i == EditorInfo.IME_ACTION_SEARCH) {

    //先隐藏键盘

    ((InputMethodManager) SearchActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE))

    .hideSoftInputFromWindow(

    SearchActivity.this

    .getCurrentFocus()

    .getWindowToken(),

    InputMethodManager.HIDE_NOT_ALWAYS);

    search();

    return true;

    }

    return false;

    }

    });

    相关文章

      网友评论

          本文标题:软键盘实现搜索功能是跳转activity打开两次

          本文链接:https://www.haomeiwen.com/subject/hpfcnttx.html