单击按钮时,请关注菜单中的searchview

时间:2016-10-04 11:43:58

标签: android menu menuitem searchview

菜单中有一个搜索视图,我有一个按钮。单击按钮时,我需要打开搜索视图以进行搜索,这意味着焦点应该在searchview上以输入搜索文本。

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_fooddoof, menu);

     searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
       TextView searchText = (TextView)
            searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/ProximaNovaRgRegular.ttf");
    searchText.setTypeface(myCustomFont);


   searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
       @Override
       public boolean onQueryTextSubmit(String query) {
               Intent intent = new Intent(descStore.this, listdisplay.class);
               intent.putExtra("deskey", query);
               startActivity(intent);
               return false;

       }

       @Override
       public boolean onQueryTextChange(String newText) {
           //android.widget.Filter filter = descAdapter.getFilter();
           //filter.filter(newText);
           return true;

       }
   });

   searchView.setSubmitButtonEnabled(true);
   return true;

}

点击按钮时:

 @Override
public void onClick(View v) {
    if (v == mViews.description1) {
        searchview.seticonified(false);
    }

}

我尝试过seticonified选项,但它无效。请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以按以下方式请求searchview的焦点

 @Override
        public void onClick(View v) {
              switch (view.getId()) {
                case R.id.your_button_id:
                  searchview.seticonified(false);
                  searchview.requestFocus(); //request focus of the view
                break;
            }
        }