search

Home  >  Q&A  >  body text

android - ToolBar中的SearchView如何让点击之后跳转到一个新的Activity

现在在研究Toolbar,碰到一个问题。
ToolBar上有一个SearchView,就是那个放大镜,如何做才能让他像微信那样点击SearchView之后跳转到一个新的Activity(Fragment?)
加一个点击事件吗?还是Toolbar中本身就有这种属性?

新手学习中,望大神不吝赐教,谢谢!

阿神阿神2772 days ago1054

reply all(3)I'll reply

  • 阿神

    阿神2017-04-17 13:08:30

    ToolBar can be used as ActionBar, so when you setSupportActionBar(toolbar); in Activity onCreate, you can directly use the actionbar operation method. Of course, the previous navigation can no longer be used.

    SearchView should be defined on the menu. After inflate in onCreateOptionsMenu(Menu menu) mounts the meunu.xml file, you can get this button through MenuItem searchViewButton = (MenuItem) menu.findItem(R.id.menu_search);, and then you can naturally give it Bind various events, such as onClickListener and so on.

    In this way, you can jump to any activity you want by opening an intent. It’s all up to you.

    However, generally when using SearchView, you add a android:actionViewClass="android.widget.SearchView" to the menu.

    We also recommend searchView.setSearchableInfo(searchManager) to jump to the activity of search results.

    PS: My WeChat click on SearchView does not seem to jump, it just has an extra layer of shadow.

    I just tried the latest version of WeChat, and sure enough it jumps to a new Activity, which is what it should do

    java@Override
    public boolean onCreateOptionsMenu(Menu menu){
        new MenuInflater(this).inflate(R.menu.yousuu_menu,menu);
        MenuItem  searchViewButton = (MenuItem) menu.findItem(R.id.menu_search);
        searchViewButton.setClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent= new Intent(MainActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });
        // others
    }
    

    In fact, everyone did this before actionViewClass existed.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:08:30

    @李citation's answer includes key information. I will add some details and correct some errors in details.

    About "menu item click event"

    We need to override onCreateOptionsMenu to initialize the Activity/Fragment option menu.
    Android also provides another method onOptionsItemSelected(), which is called when the menu item is clicked, where the click event of the menu item is processed. Therefore, the callback for an item is generally not implemented in onCreateOptionsMenu.
    What needs to be corrected is that MenuItem does not have setClickListener, but MenuItem.setOnMenuItemClickListener.

    So, we usually do it like this:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_start_search_activity, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_item_search:
            startActivity(new Intent(StartSearchActivity.this, PhotoGalleryActivity.class));        
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    About "Jump to a new Activity after clicking SearchView"

    When we want to add a search box to an activity (in other words, click on the action bar menu item, and then the search box pops up on the action bar), the general approach is to add the activity (that is, the activity that contains the search menu item) Configure it to be searchable, and then implement the corresponding method.
    Depending on whether the menu item is configured with android:actionViewClass="android.widget.SearchView", the corresponding methods (onCreateOptionsMenu, onOptionsItemSelected) have some differences in implementation.
    There are also differences in the results. The former covers a search dialog box on the action bar, while the latter has the same style and theme as the application, as shown below:

    So, if you want to jump to a new Activity after clicking SearchView, it will be like the effect of the WeChat search box.
    Need to do this:

    1. First configure this new Activity as a Searchable Activity, and implement its corresponding methods;
      Refer to (1) Configure a Searchable Activity; initiate a search request; in onNewIntent Process the search intent; (2) Use SearchView as the designated view for the search menu item.

    2. Then add a MenuItem to a certain Activity. In the click event of the Item (refer to the above: About the "click event of the menu item"), start the activity configured above;
      Therefore, it needs to be corrected The problem is that this item is an ordinary item, not an item with a SearchView. Your question statement is wrong on this point. Reference Start a searchable Activity; Start a searchable activity through MenuItem.

    3. At the same time, please note that you need to call searchView.setIconifiedByDefault(false) so that the searchView of the launched searchable activity is expanded, as shown in the following GIF effect:

    Done :)

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:08:30

    Thank you to the man upstairs for solving my problem

    reply
    0
  • Cancelreply