本文介绍了菜单项在android eclipse中单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android开发(日食)中非常新.我尝试做以下事情.1.在日食中单击菜单按钮时,会生成一个菜单列表2.单击菜单项时,浏览另一个页面(xml页面).我使用以下代码.

i am very new in android development (eclipse).i am try to do the following things.1. generate a menu list when click on menu button in the eclipse2. when clicking on the menu item navigate another page(xml page). i use the following codes.

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/about" android:title="About"/>
<item android:id="@+id/help" android:title="Help" />
</menu>

MainActivity.java

MainActivity.java

package com.example.menuoptions;

import com.example.menuoptions.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.about:
    startActivity(new Intent(this, About.class));
    return true;
    case R.id.help:
    startActivity(new Intent(this, Help.class));
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
}
}

About.java

About.java

 package com.example.menuoptions;
 import android.app.Activity;
 import android.os.Bundle;
 public class About extends Activity {
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);

   }
  }

about.xml

about.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="About Page" />

</LinearLayout>

问题是我的第二个需要.列出了菜单项,但是单击关于"项时,它不会导航到about.xml.

Problem is in my second need. The menu items are listed but when clicking on the about item it is not navigate to about.xml.

推荐答案

您应使用此代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
    return super.onOptionsItemSelected(item);
}
}

这篇关于菜单项在android eclipse中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:03