Android 主页按钮的操作栏 onClick 侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11079718/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:56:00  来源:igfitidea点击:

Action Bar's onClick listener for the Home button

androidandroid-intentactionbarsherlockonitemclicklistener

提问by noloman

How can I implement a custom onClickListenerfor the Home button of the Action Bar?

如何onClickListener为操作栏的主页按钮实现自定义?

I already did a getSupportActionBar().setDisplayHomeAsUpEnabled(true);and now I want to redirect the user to a certain activity in case the Home button is clicked.

我已经做了一个getSupportActionBar().setDisplayHomeAsUpEnabled(true);,现在我想将用户重定向到某个活动,以防点击主页按钮。

I tried with:

我试过:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    Intent i = new Intent();
                    i.setClass(BestemmingActivity.this, StartActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    return true;
                }
            });
        default:
            return super.onOptionsItemSelected(item);
        }
    }

but it never enters in the onMenuItemClick.

但它永远不会进入onMenuItemClick.

Basically, it's done just like in this linkbut still it doesn't enter in the listener.

基本上,它就像在这个链接中一样完成,但它仍然没有进入侦听器。

采纳答案by noloman

Fixed: no need to use a setOnMenuItemClickListener. Just pressing the button, it creates and launches the activity through the intent.

固定:无需使用setOnMenuItemClickListener. 只需按下按钮,它就会通过意图创建并启动活动。

Thanks a lot everybody for your help!

非常感谢大家的帮助!

回答by lynn8570

I use the actionBarSherlock, after we set supportActionBar.setHomeButtonEnabled(true);
we can override the onMenuItemSelected method:

我用的是actionBarSherlock,之后我们设置supportActionBar.setHomeButtonEnabled(true);
我们可以覆盖onMenuItemSelected方法:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
        toggle();

        // Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
        break;

    }

    return true;
}

I hope this work for you ~~~ good luck

我希望这对你有用~~~祝你好运

回答by Saad Mahmud

if anyone else need the solution

如果其他人需要解决方案

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home) {
        onBackPressed();  return true;
    }

    return super.onOptionsItemSelected(item);
}

回答by raju

if we use the system given action bar following code works fine

如果我们使用系统给定的操作栏以下代码工作正常

getActionBar().setHomeButtonEnabled(true);

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
      //do your action here.
        break;

    }

    return true;
}

回答by Nikolay Elenkov

You need to explicitly enable the home action if running on ICS. From the docs:

如果在 ICS 上运行,您需要显式启用 home 操作。从文档

Note: If you're using the icon to navigate to the home activity, beware that beginning with Android 4.0 (API level 14), you must explicitly enable the icon as an action item by calling setHomeButtonEnabled(true) (in previous versions, the icon was enabled as an action item by default).

注意:如果您使用图标导航到主页 Activity,请注意从 Android 4.0(API 级别 14)开始,您必须通过调用 setHomeButtonEnabled(true) 将图标显式启用为操作项(在以前的版本中,默认情况下,图标已作为操作项启用)。

回答by Naeem Ibrahim

Best way to customize Action bar onClickListener is onSupportNavigateUp()

自定义操作栏 onClickListener 的最佳方法是 onSupportNavigateUp()

This code will be helpful link for helping code

此代码将是帮助代码的有用链接

回答by David

answers in half part of what is happening. if onOptionsItemSelectednot control homeAsUpbutton when parent activity sets in manifest.xml system goes to parent activity. use like this in activity tag:

回答了正在发生的事情的一半。如果在 manifest.xml 系统中设置父活动时onOptionsItemSelected没有控制homeAsUp按钮转到父活动。在活动标签中像这样使用:

<activity ... >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.activities.MainActivity" /> 
</activity>

回答by Soha Soha

you should to delete your the Override onOptionsItemSelected and replate your onCreateOptionsMenu with this code

您应该删除您的 Override onOptionsItemSelected 并使用此代码重新安装您的 onCreateOptionsMenu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_action_bar_finish_order_stop, menu);
        menu.getItem(0).setOnMenuItemClickListener(new FinishOrderStopListener(this, getApplication(), selectedChild));
        return true;

    }