android - 当我在操作栏上添加溢出菜单时,操作向上按钮不起作用

时间:2017-05-02 10:08:37

标签: java android

我的Android应用程序中有一个操作栏向上按钮和溢出菜单。溢出菜单工作正常,但操作向上按钮根本不起作用。完全没有错误,因为日志没有显示任何内容。

以下是我的溢出菜单代码按钮:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:checkableBehavior="none">
        <item
            android:id="@+id/emailPdf"
            android:title="Email" />
        <item
            android:id="@+id/printPdf"
            android:title="Print" />

    </group>
</menu>

而且,它的java代码是:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_action_bar_menu_options, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        super.onOptionsItemSelected(item);

        switch(item.getItemId()){
            case R.id.email:
                Toast.makeText(getBaseContext(), "You selected Email", Toast.LENGTH_SHORT).show();
                break;

            case R.id.print:
                Toast.makeText(getBaseContext(), "You selected Print", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;

    }

此外,操作向上按钮的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

Android Manifest文件中的代码:

   <activity
            android:name=".ChildActivity"
            android:label="@string/title_activity_child"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.sample.sample.MainActivity" />
      </activity>

当我删除Overflow Menu的java代码时,只有Action Up Button可以工作。 我不知道为什么会这样。帮助将不胜感激。

提前致谢。

2 个答案:

答案 0 :(得分:0)

将onOptionsItemSelected代码更改为此。

   @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                if (item.getItemId() == android.R.id.home) {
                    onBackPressed();
                    return true;
                }else if(item.getItemId() == R.id.email){
                        Toast.makeText(getBaseContext(), "You selected Email", Toast.LENGTH_SHORT).show();

                }else if(item.getItemId() == R.id.print){
                         Toast.makeText(getBaseContext(), "You selected Print", Toast.LENGTH_SHORT).show();

                }
                return super.onOptionsItemSelected(item);
            }

答案 1 :(得分:0)

@Override public boolean onOptionsItemSelected(MenuItem item){

super.onOptionsItemSelected(item);

switch(item.getItemId()){
    case R.id.email:
        Toast.makeText(getBaseContext(), "You selected Email", Toast.LENGTH_SHORT).show();
        break;

    case R.id.print:
        Toast.makeText(getBaseContext(), "You selected Print", Toast.LENGTH_SHORT).show();
        break;
    case R.id.home:
      // Simulate back button press.
         onBackPressed();
         break;
}
return true;

}