-1

我制作了一个包含很多按钮和活动的应用程序。尽管我无法理解如何通过菜单中的按钮(当在电话上单击菜单按钮时)(充气菜单)来启动新活动。这是连接到我的活动的菜单的代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1" android:title="@string/menu_home"></item>
</menu>

这是我在 Java 中的活动:

package com.gmail.derekcraigsmith.nanaimobus;

import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;

public class Route1TimesCcMonfriAActivity extends Activity implements            

OnMenuItemClickListener {

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

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.route1_times_cc_monfri_a, menu);
    return true;
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    // TODO Auto-generated method stub
    return false;
}
}
4

2 回答 2

0

菜单文件夹中的 activity_main.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:id="@+id/next"
      android:title="Next" />
 </menu>

在你的活动中膨胀菜单

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{

switch (item.getItemId()) //get the id which is an int
{
case R.id.next:  // check if its the menu item next selected
    // Single menu item is selected do something
    // Ex: launching new activity/screen or show alert message
    Toast.makeText(MainActivity.this, "Next Selected", Toast.LENGTH_SHORT).show();
    startActivity(new Intent(MainActivity.this,secondAct.class));//start activity
    break;

default:
    return super.onOptionsItemSelected(item);   
}
}
于 2013-04-17T07:03:56.080 回答
0
int id = item.getItemId(); 
  switch (id) {
    case R.id.your_menu_item_id : {
       startActivity(new Intent(start_activity, next_activity));

 }

其中 start activity 是您的主要活动, next_activity 是您要开始的活动。在这里查看我的博客以获取更多信息。

于 2013-04-17T07:06:32.263 回答