简体   繁体   中英

Android: Actionbar item onclick

1-To add a search item to my actionbar i have the item in this way:

<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");

    }

,but if i add this one android:onClick="action_search" it gives me these errors

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" ? What is it doing?

1. You shouldn't add onClicks to ActionMenu items that way. Instead, you have to override onOptionsItemSelected like so:

@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:

@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. 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

The way to process action bar clicks in your Activity is as following:

@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.

ActionBar Menu items are created using

@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. 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

and ifRoom tag means: the specified item will be visible only if there is space available on the action bar.

And dont use onClick listener in this senario, onOptionItemSelected will be perfect. Its all well documented in the mentioned link.

  1. In your Activity click of menu item will be handeled here. .

@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

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

ifRoom Only place this item in the Action Bar if there is room for it.

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,

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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