单击设置图标创建新活动

Luf*_*ffy 4 android menu android-actionbar

如果我单击该settings图标,则必须创建新活动.但它显示弹出搜索.我发布了相关的代码和屏幕截图.

在此输入图像描述

在此输入图像描述

MainActivity.java:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       switch (item.getItemId()) 
       {
         case R.id.search:
            Intent intent = new Intent(this, SettingsActivity.class);
      //      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            return true;
         default:
            return super.onOptionsItemSelected(item);
       }
    }
Run Code Online (Sandbox Code Playgroud)

main.xml中:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >

    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/search"/>

</menu>
Run Code Online (Sandbox Code Playgroud)

如果我点击该图标,则必须创建新活动.但它显示弹出窗口search.我不需要弹出窗口.只需单击设置图标就可以移动到新活动.到目前为止,我完成了像这样的代码.

小智 5

您必须添加项目标记

android:showAsAction="always"

然后没有弹出窗口.