将软键盘变成搜索

首先需要在edittext里面配置


android:imeOptions="actionSearch"
android:imeActionLabel="@string/search"
android:singleLine="true"


然后在java代码里面监听软键盘搜索键的回调


binding.searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId,KeyEvent event)  {
        if (actionId== EditorInfo.IME_ACTION_SEND || (event!=null&&event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
            search();
            return true;
        }
        return false;
    }

});

你可能感兴趣的:(android)