第二个活动标题栏中的后退按钮

时间:2016-01-18 20:12:16

标签: java android xml

我想在第二个活动的标题栏中添加后退按钮...这是我当前的代码...当我点击一个活动....不幸的是ActionBarMenu有停止...我不知道什么是错的在它...请帮助我...主要的Acticity:

package com.example.hamza.actionbarmenu;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @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_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement  if(id == R.id.spinner)
        {
            Intent intent = new Intent(this, secondActivity.class);
            startActivity(intent);
        }




        return super.onOptionsItemSelected(item);
    }
}




Second Activity...


package com.example.hamza.actionbarmenu;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;

public class secondActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


menu_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"
    tools:context="com.example.hamza.actionbarmenu.MainActivity">

    <item
        android:id="@+id/spinner"
        android:title="@string/title"
        android:actionViewClass="android.widget.Spinner"
        android:showAsAction="ifRoom"
        />

    <item
        android:id="@+id/Orange"
        android:title="@string/Fruit"
        android:actionViewClass="android.widget.Spinner"
        android:showAsAction="ifRoom"
        />

    <item
        android:id="@+id/Apple"
        android:title="@string/select"
        android:actionViewClass="android.widget.Spinner"
        android:showAsAction="ifRoom"
        />


</menu>



String.xml ....


<resources>
    <string name="app_name">ActionBarMenu</string>
    <string name="action_settings">Settings</string>
    <string name="title">Mango</string>
    <string name="select">Apple</string>
    <string name="Fruit">Orange</string>
    <string name="title_activity_third">You Select Apple</string>
    <string name="title_activity_second">You Select Mango</string>
    <string name="title_activity_fourth">You Select Orange</string>
</resources>


Android Manifest.xml ...


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hamza.actionbarmenu">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.hamza.actionbarmenu.MainActivity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".secondActivity"
            android:label="@string/title_activity_second"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

如果您使用的是Android Studio,则通过向导创建活动是确保一切正常运行的最佳方法。 Android在设备中加入了一个内置的后退按钮,所以你不必担心把它放在一个小块中。

答案 1 :(得分:-1)

创建新的Blank Activity' and copy your code in it.空白活动have a working返回“按钮。