getActionView is deprecated?

前端 未结 4 917
青春惊慌失措
青春惊慌失措 2021-01-01 09:54

Today I decide to translate my android app from Java to Kotlin ! :) But I was very surprise when I type this :

val searchItem = menu.findItem(R.id.action_sea         


        
相关标签:
4条回答
  • 2021-01-01 10:33

    The Javadoc says:

    Use getActionView() directly.

    Hence, what you should do is:

    val searchView = searchItem.getActionView() as SearchView
    
    0 讨论(0)
  • 2021-01-01 10:43

    Use actionView directly in Kotlin, like this:

      override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.actions, menu)
        val searchItem = menu?.findItem(R.id.action_search)
        val searchView = searchItem?.actionView as SearchView
        searchView.animate()
        // TODO: Configure the search info and add any event listeners...
        return super.onCreateOptionsMenu(menu)
    }
    
    0 讨论(0)
  • 2021-01-01 10:48

    You can use the same as provided on android developer website

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
    
        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
    
        return true;
    }
    
    0 讨论(0)
  • 2021-01-01 10:56

    As suggested by egor, you can do like this

        getMenuInflater().inflate(R.menu.menu_items, menu);
        MenuItem menuItem = menu.findItem(R.id.action_search);
    
        SearchView searchView = (SearchView) menuItem.getActionView();
        search(searchView);
        return true;
    
    0 讨论(0)
提交回复
热议问题