1 - 要将搜索项添加到 actionbar ,我就会以这种方式获得该项:
<item
android:id="@+id/search"
android:icon="@drawable/ic_action_search"
android:title="@string/Search"
android:showAsAction="ifRoom"
/>
// onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
// OnOptionItemSelected
public boolean OnOptionItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.search:
action_search();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// SEARCH_ACTION
public void action_search()
{
System.out.println("Heeeeey");
}
,但是如果我添加这个android:onClick="action_search"
,它会给我这些错误
03-20 07:40:12.622: E/Trace(1277): error opening trace file: No such file or directory (2)
03-20 07:40:13.002: E/AndroidRuntime(1277): FATAL EXCEPTION: main
03-20 07:40:13.002: E/AndroidRuntime(1277): android.view.InflateException: Couldn't resolve menu item onClick handler action_search in class com.example.lesson1.MainActivity
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:217)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:417)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater.parseMenu(MenuInflater.java:188)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater.inflate(MenuInflater.java:110)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.example.lesson1.MainActivity.onCreateOptionsMenu(MainActivity.java:30)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2913)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.os.Handler.handleCallback(Handler.java:615)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.os.Looper.loop(Looper.java:137)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-20 07:40:13.002: E/AndroidRuntime(1277): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 07:40:13.002: E/AndroidRuntime(1277): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-20 07:40:13.002: E/AndroidRuntime(1277): at dalvik.system.NativeStart.main(Native Method)
03-20 07:40:13.002: E/AndroidRuntime(1277): Caused by: java.lang.NoSuchMethodException: action_search [interface android.view.MenuItem]
03-20 07:40:13.002: E/AndroidRuntime(1277): at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-20 07:40:13.002: E/AndroidRuntime(1277): at java.lang.Class.getMethod(Class.java:915)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:215)
03-20 07:40:13.002: E/AndroidRuntime(1277): ... 18 more
2 - 什么是android:showAsAction="ifRoom"
?它在做什么?
答案 0 :(得分:3)
1。您不应该通过这种方式将onClick添加到ActionMenu项目。相反,您必须像这样覆盖onOptionsItemSelected
:
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.search:
// your action goes here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
并将您的布局扩展到ActionBar中,如下所示:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
2. ifRoom
表示如果有空格,请将操作项放在操作栏上。但是,空间由以下因素决定:小于动作栏水平空间宽度的一半,并且计数小于动作项目的最大数量 - 杰克沃顿。
Have a look here for more information on the android ActionBar
答案 1 :(得分:2)
处理Activity
中操作栏点击的方法如下:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int id = item.getItemId();
if (R.id.search == id) {
// do something and maybe return true...
}
return super.onOptionsItemSelected(item);
}
ifRoom
标记表示如果操作栏中的项目空间不足,则会出现在操作溢出中。
答案 2 :(得分:0)
ActionBar菜单项使用
创建@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
然后使用
传递项目的点击事件@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
//TODO do your thing.
return true;
default:
return false;
}
}
还有一件事, ifRoom表示如果有空间,该项目将显示为图标。如果您只有一个项目,则总会有一个房间。如果没有空间,则该项目将显示在溢出菜单中。
答案 3 :(得分:0)
您需要按照LINK
中提到的完整教程进行操作和 ifRoom 标记表示:只有在操作栏上有可用空间时,指定的项才会显示。
并且在此场景中不使用onClick 侦听器, onOptionItemSelected 将是完美的。它在所提到的链接中都有很好的记录。
答案 4 :(得分:0)
Activity
点击菜单项中,将会在这里进行处理。
@覆盖
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.search:
//do your work here
return true;
}
}
2。
android:showAsAction
关键字。何时以及如何将此项目显示为操作栏中的操作项。仅当活动包含ActionBar
时,菜单项才会显示为操作项 ifRoom
只有在有空间时才将此项目放在操作栏中。
您可以获取详细信息here
答案 5 :(得分:0)
如果你使用的是Android 11及更高版本,你可以在你的活动中使用android searchview小部件,如下所示,
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_websearch"
android:actionViewClass="android.widget.SearchView"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="always"
android:title="@string/search_title"/>
</menu>
和,
<强> onCreateOptionsMenu()强>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.main, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_websearch)
.getActionView();
}