简体   繁体   English

Android:动作栏项目onclick

[英]Android: Actionbar item onclick

1-To add a search item to my actionbar i have the item in this way: 1-要将搜索项添加到我的操作栏中,我可以通过以下方式获得该项:

<item 

      android:id="@+id/search"
      android:icon="@drawable/ic_action_search" 
      android:title="@string/Search"
      android:showAsAction="ifRoom"    
      />

//onCreateOptionsMenu // 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 // OnOptionItemSelected

public boolean OnOptionItemSelected(MenuItem item)
{

    switch(item.getItemId())
    {

      case R.id.search:
          action_search();
          return true;  

      default:
         return super.onOptionsItemSelected(item);


    }   
}

//Search_Action // SEARCH_ACTION

public void action_search()
    {

        System.out.println("Heeeeey");

    }

,but if i add this one android:onClick="action_search" it gives me these errors ,但是如果我添加一个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-What is android:showAsAction="ifRoom" ? 2-什么是android:showAsAction="ifRoom" What is it doing? 到底在做什么

1. You shouldn't add onClicks to ActionMenu items that way. 1.您不应以这种方式将onClicks添加到ActionMenu项。 Instead, you have to override onOptionsItemSelected like so: 相反,您必须像这样重写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);
    }
}

And inflate your layout into the ActionBar as the following: 并按如下所示将布局添加到ActionBar中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mymenu, menu);
    return true;
}

2. ifRoom means place the action item on the action bar if there is space. 2. ifRoom表示如果有空间,请将操作项放在操作栏上。 However, space is determined by the following: less than half the width of the action bar horizontal space and the count is less than the max number of action items - Jake Wharton. 但是,空间由以下方式确定:小于操作栏水平空间宽度的一半,并且计数小于操作项的最大数量-杰克·沃顿。

Have a look here for more information on the android ActionBar 在这里查看有关Android ActionBar的更多信息

The way to process action bar clicks in your Activity is as following: 处理活动栏在“ 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);
}

The ifRoom tag means that if there's not enough room for the item in the action bar, it will appear in the action overflow. ifRoom标记表示如果操作栏中的项目空间不足,它将显示在操作溢出中。

ActionBar Menu items are created using 使用以下命令创建ActionBar菜单项

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    return true;
}

And then the click events of the items are delivered using 然后使用以下方式传递项目的点击事件

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.search:
        //TODO do your thing.
        return true;
    default:
        return false;
    }
}

One more thing, ifRoom means that the item is displayed as an icon if there is room. 还有一点,ifRoom表示如果有空间,则该项目显示为图标。 If you have only one item, there is a room always. 如果您只有一件物品,那么总会有一个房间。 If there is not room, the item is displayed in overflow menu. 如果没有空间,则该项目显示在溢出菜单中。

You need to follow the complete tutorial as mentioned in that LINK 您需要按照LINK中提到的完整教程进行操作

and ifRoom tag means: the specified item will be visible only if there is space available on the action bar. 并且ifRoom标记表示:仅当操作栏上有可用空间时,指定的项目才可见。

And dont use onClick listener in this senario, onOptionItemSelected will be perfect. 并且不要在此版本中使用onClick侦听器, onOptionItemSelected将是完美的。 Its all well documented in the mentioned link. 在提到的链接中都有详细记录。

  1. In your Activity click of menu item will be handeled here. 在您的Activity ,将在此处单击菜单项。 .

@Override @覆盖

 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 2. android:showAsAction

Keyword. 关键词。 When and how this item should appear as an action item in the Action Bar. 该项目何时以及如何在操作栏中显示为操作项目。 A menu item can appear as an action item only when the activity includes an ActionBar 仅当活动包含ActionBar时,菜单项才能显示为操作项

ifRoom Only place this item in the Action Bar if there is room for it. ifRoom仅在有ifRoom才将其放在操作栏中。

you can get details here 您可以在此处获取详细信息

if you are using android version 11 and above , you can use android searchview widget in your activity like below, 如果您使用的是Android 11及更高版本,则可以在如下所示的活动中使用android searchview小部件,

main.xml 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>

and ,

onCreateOptionsMenu() 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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM