简体   繁体   中英

Action bar option action

Before Clicking:

点击之前

After Clicking:

点击后

I am having an issue with my code where clicking on an actionbar action will bring up the title of the action. If you then click on the title that is how you get to the onclick listener. Very strange... I know it is hard to tell without code but I am away and will post it soon. Just wanted to ask around to see if anyone has experienced similar.

ActionBar:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_settings"
    android:title="@string/hello1"
    app:showAsAction="never"/>

</menu>

onClickListener:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items

    switch (item.getItemId()) {
        case R.id.action_settings:
            onCreateDialog();
            return true;
        case android.R.id.home:
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

public void onCreateDialog() {
    String[] string= new String[]{"Add to Calendar", "Share"};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Options")
            .setItems(string, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:...................}

This is the expected behavior for the way you have the menu setup currently.

<item
    android:id="@+id/action_settings"
    android:title="@string/hello1"
    app:showAsAction="never"/> <!-- Don't show this action until the overflow menu is shown -->

With the showAsAction set to never , the action is not shown until the overflow menu is shown. The available options for showAsAction are:

["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]

In your case, because you only have one menu item, you can use always .

Refer to the menu resource documentation for more info.

You should be able to preview this in Android Studio (see screenshot below)

Android Studio预览版

I fixed it by extending the activity to appcompatactivity instead of activity. Weird bug. Then it didn't do it.

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