在操作栏主页按钮按钮、操作、主页

2023-09-12 21:57:25 作者:勿忘心安 ﹌

我是新android开发,并继添加操作栏教程

I am new to android development and was following the "Adding Action Bar Tutorial"

我一直想把家里(背胡萝卜)按钮的操作栏,使我的应用程序返回的主要活动。

I was trying to get the home (back carrot) button on the action bar to enable my app to go back to the main activity.

据提到的教程,这可能不处理了按钮的事件来完成,若该家长在XML中定义。

It is mentioned in the tutorial that this can be done without handling the up button event, if the parent is defined in the xml.

下面是我的xml文件

<activity
    android:name="com.example.firstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>

但pressing胡萝卜按钮不起作用。我能是做错了什么?

but pressing the carrot button does not work. what can i be doing wrong?

编辑: 添加以下

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
         * setContentView(R.layout.activity_display_message); if
         * (savedInstanceState == null) {
         * getSupportFragmentManager().beginTransaction() .add(R.id.container,
         * new PlaceholderFragment()).commit(); }
         */

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
        getSupportActionBar().setDisplayHomeAsUpEnabled(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.
        /*
         * switch (item.getItemId()) { case android.R.id.home: Intent intent =
         * new Intent(getApplicationContext(),
         * com.example.firstapp.MainActivity.class);
         * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         * startActivity(intent); return true; default: return
         * super.onOptionsItemSelected(item); }
         */
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_display_message,
                    container, false);
            return rootView;
        }
    }

}

MainActivity低于

MainActivity is below

public class MainActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                // openSearch();
                EditText temp1 = (EditText) findViewById(R.id.edit_message);
                temp1.setText("Search");
                return true;
            case R.id.action_settings:
                // openSettings();
                EditText temp2 = (EditText) findViewById(R.id.edit_message);
                temp2.setText("Settings");
                return true;
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        // Do something in response to button
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

感谢

推荐答案

@Newton它实际上是多余的调用ActionBar.setDisplayHomeAsUpEnabled(真),如果你使用的parentActivityName属性..这就是说,当你删除该行从你的DisplayMessageActivity,就是涨启示仍然present? - adneal

@Newton It's actually redundant to call ActionBar.setDisplayHomeAsUpEnabled(true), if you're using the parentActivityName attribute.. That being said, when you remove that line from your DisplayMessageActivity, is the "up" affordance still present? – adneal