我已经尝试了所有的东西,但我无法创建选项菜单。你能给我完整的代码吗?我应该在清单文件中进行任何更改以创建菜单吗?我应该只为菜单创建另一个类,还是我可以在之前创建的任何类中编写它?请帮助我。我这样写:
package com.example.FirstProject;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
//import android.widget.EditText;
//import android.widget.RadioButton;
//import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class FirstProjectActivity extends Activity {
/** Called when the activity is first created. */
protected ListAdapter adapter;
Cursor cursor;
protected String[] cities = {"Mumbai"};
ListView lv ;
ListView stations;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void myClickHandler(View view)
{
Intent i=new Intent(this,City.class);
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
{
if( R.id.icon==item.getItemId()) Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
if( R.id.icon==item.getItemId()) Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
if( R.id.icon==item.getItemId()) Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
}
return true;
}
}
答案 0 :(得分:0)
试试这个...
public boolean onOptionsItemSelected(MenuItem item)
{
System.out.println("entered to option selected");
switch(item.getItemId())
{
case R.id.icon:
System.out.println("entered to icon");
Toast.makeText(this, "icon clicked", Toast.LENGTH_LONG).show();
break;
case R.id.text:
Toast.makeText(this, "text clicked", Toast.LENGTH_LONG).show();
break;
case R.id.iconandtext:
Toast.makeText(this, "icon and text clicked", Toast.LENGTH_LONG).show();
break;
}
return true;
}
请参阅此链接..它也很有用enter link description here
答案 1 :(得分:0)
使用如下代码:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Item1");
menu.add("Item2");
return true;
}
答案 2 :(得分:0)
您可以在menu.xml
中使用以下代码<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu1"
android:alphabeticShortcut="s"
android:title="menu1 title"
/>
<item
android:id="@+id/menu2"
android:alphabeticShortcut="t"
android:title="menu2 title"
/>
</menu>
现在调用菜单和函数的两种方法是在所选项目上调用。
@override
public boolean onCreateOptionsMenu (Menu menu){
super.onCreateOptionsMenu(menu);
//menu.add(R.menu.main_menu);
//menu.add(Menu.NONE, 101,Menu.FIRST,this.getResources().getString(R.string.Title));
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.main_menu, menu);
return true;
}
您可以使用调用菜单的方法。第一个是menu.add,第二个是通过膨胀menu.xml文件 现在,itemselectedlistner由以下代码
使用public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menu1:
// your code here
case R.id.menu2:
// your code here
}
return false;
}
只需在switch-case中传递id,然后通过选择那些菜单来执行任何操作。