java getActionView 已弃用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45306133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getActionView is deprecated?
提问by Ross Thomas
Today I decide to translate my android app from Java to Kotlin ! :) But I was very surprise when I type this :
今天我决定将我的 android 应用程序从 Java 翻译成 Kotlin!:) 但是当我输入这个时我很惊讶:
val searchItem = menu.findItem(R.id.action_search)
val searchView = MenuItemCompat.getActionView(searchItem) as SearchView
And Android Studio told me : " 'getActionView(MenuItem!):View!' is deprecated. Deprecated in Java "
Android Studio 告诉我:“'getActionView(MenuItem!):View!' 已弃用。在 Java 中已弃用“
So before to ask you the solution I ask to Google what is the solution and I believed I find the solution : "Use getActionView() directly."
所以在问你解决方案之前,我问谷歌什么是解决方案,我相信我找到了解决方案:“直接使用 getActionView()”。
So I modified my code like this :
所以我像这样修改了我的代码:
val searchView = MenuItemCompat.getActionView() as SearchView
But getActionView()
is still crossed so I don't understand at all...
但是getActionView()
还是被划掉了所以我完全不明白......
I will be very happy if you can help me :) Thank you !
如果你能帮助我,我会很高兴:) 谢谢!
回答by Egor
The Javadoc says:
Javadoc 说:
Use getActionView() directly.
直接使用 getActionView()。
Hence, what you should do is:
因此,你应该做的是:
val searchView = searchItem.getActionView() as SearchView
回答by Ashish Kumar
As suggested by egor, you can do like this
正如 egor 所建议的,你可以这样做
getMenuInflater().inflate(R.menu.menu_items, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
search(searchView);
return true;
回答by JoboFive
You can use the same as provided on android developerwebsite
您可以使用与android 开发者网站上提供的相同的
@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;
}
回答by Harry Zhang
Use actionView
directly in Kotlin, like this:
actionView
在 Kotlin 中直接使用,像这样:
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)
}