简体   繁体   English

菜单项单击android eclipse

[英]Menu Item Click in android eclipse

i am very new in android development (eclipse).i am try to do the following things. 我在android开发(eclipse)中非常新。我尝试做以下事情。 1. generate a menu list when click on menu button in the eclipse 2. when clicking on the menu item navigate another page(xml page). 1.在Eclipse中单击菜单按钮时,会生成一个菜单列表。2.在菜单项上单击时,导航到另一个页面(xml页面)。 i use the following codes. 我使用以下代码。

menu.xml 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 关于.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>

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. 列出了菜单项,但是单击“关于”项时,不会导航到about.xml。

you should use this: 您应该使用此:

@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);
}
}

您是否在清单文件中提供了许可?

 <activity android:name=".About" />
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

simple code for menu selected 选择菜单的简单代码

@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);
    }
}

Try to use this 尝试使用这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM