2

我做了以下但没有奏效

@Override
    public boolean onOptionsItemSelected(MenuItem menuItem)
    {       

        startActivity(new Intent(NamesList.this,MainActivity.class)); 

        switch (menuItem.getItemId()) {
        case 1:
            search = (EditText) menuItem.getActionView();
            search.addTextChangedListener(filterTextWatcher);
            search.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

            String result = search.getText().toString();

            Toast.makeText(getApplicationContext(), "the :: " + result, Toast.LENGTH_LONG);

        }   


        return true;

    }
4

1 回答 1

1

您可以使用search.setOnQueryTextListener()为 SearchView 中的用户操作设置侦听器。

search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            //show your toast here
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

search.setOnSearchClickListener()设置侦听器以在按下搜索按钮时通知。这仅在默认情况下不可见文本字段时才相关。

于 2012-12-18T08:28:15.417 回答