我想等到这样的目的:public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
System.out.println(item.getItemId()); if (item.getItemId() == 0) {
System.out.println("intreasting");
} else if (item.getItemId()==1) {
System.out.println("intreasting");
} return super.onOptionsItemSelected(item);
}但是没有结果,item.getItemId()返回的到底是什么了,我改怎么做啊,谢谢
menu是用xml生成的
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/item_a"
android:icon="@drawable/gimp"
android:title="Gimp">
</item></menu>
具体代码如下package gj.android.eyesight;import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;public class EyesightMain extends TabActivity implements OnTabChangeListener {
//声明TabHost对象
private TabHost mTabHost;
protected int myMenuSettingTag=0;
protected Menu myMenu;
private static final int myMenuResources[] = { R.menu.a_menu,R.menu.b_menu, R.menu.c_menu};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.mainlayout);
//若增加这项就需要对对局文件修改,增加tabhost
//取得TabHost对象
//mTabHost = getTabHost();
mTabHost=this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.mainlayout, mTabHost.getTabContentView(), true);
/* 为TabHost添加标签 */
//新建一个newTabSpec(newTabSpec)
//设置其标签和图标(setIndicator)
//设置内容(setContent)
mTabHost.addTab(mTabHost.newTabSpec("tab_gard")
.setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))
.setContent(R.id.gardlinarlayout));
mTabHost.addTab(mTabHost.newTabSpec("tab_test")
.setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))
.setContent(R.id.testlinarlayout));
mTabHost.addTab(mTabHost.newTabSpec("tab_set")
.setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))
.setContent(R.id.setlinarlayout));
//设置TabHost的背景颜色
mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
//设置TabHost的背景图片资源
mTabHost.setBackgroundResource(R.drawable.bg0);
//设置当前显示哪一个标签
mTabHost.setCurrentTab(0);
//标签切换事件处理,setOnTabChangedListener
/*mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
// TODO Auto-generated method stub
@Override
public void onTabChanged(String tabId)
{
Dialog dialog = new AlertDialog.Builder(EyesightMain.this)
.setTitle("提示")
.setMessage("当前选中:"+tabId+"标签")
.setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.cancel();
}
}).create();//创建按钮
dialog.show();
}
});*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
myMenu = menu;
myMenu.clear();//娓呯┖MENU鑿滃崟
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
//浠嶵abActivity杩欓噷鑾峰彇涓�釜MENU杩囨护鍣�
switch (myMenuSettingTag) {
case 1:
inflater.inflate(myMenuResources[0], menu);
//鍔ㄦ�鍔犲叆鏁扮粍涓搴旂殑XML MENU鑿滃崟
break;
case 2:
inflater.inflate(myMenuResources[1], menu);
break;
case 3:
inflater.inflate(myMenuResources[2], menu);
break;
default:
inflater.inflate(myMenuResources[0], menu);
break;
}
return super.onCreateOptionsMenu(menu);
} /*@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "即将转到另一个界面",
// Toast.LENGTH_LONG).show();
return super.onMenuItemSelected(featureId, item);
}*/ @Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
System.out.println(item.getItemId()); if (item.getItemId() == 0) {
System.out.println("intreasting");
} else if (item.getItemId()==1) {
System.out.println("intreasting");
} return super.onOptionsItemSelected(item);
} @Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
if (tabId.equals("tab_gard")) {
myMenuSettingTag = 1;
}
if (tabId.equals("tab_test")) {
myMenuSettingTag = 2;
}
if (tabId.equals("tab_set")) {
myMenuSettingTag = 3;
}
if (myMenu != null) {
onCreateOptionsMenu(myMenu);
}
}
}
// TODO Auto-generated method stub
System.out.println(item.getItemId()); if (item.getItemId() == 0) {
System.out.println("intreasting");
} else if (item.getItemId()==1) {
System.out.println("intreasting");
} return super.onOptionsItemSelected(item);
}但是没有结果,item.getItemId()返回的到底是什么了,我改怎么做啊,谢谢
menu是用xml生成的
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/item_a"
android:icon="@drawable/gimp"
android:title="Gimp">
</item></menu>
具体代码如下package gj.android.eyesight;import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;public class EyesightMain extends TabActivity implements OnTabChangeListener {
//声明TabHost对象
private TabHost mTabHost;
protected int myMenuSettingTag=0;
protected Menu myMenu;
private static final int myMenuResources[] = { R.menu.a_menu,R.menu.b_menu, R.menu.c_menu};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.mainlayout);
//若增加这项就需要对对局文件修改,增加tabhost
//取得TabHost对象
//mTabHost = getTabHost();
mTabHost=this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.mainlayout, mTabHost.getTabContentView(), true);
/* 为TabHost添加标签 */
//新建一个newTabSpec(newTabSpec)
//设置其标签和图标(setIndicator)
//设置内容(setContent)
mTabHost.addTab(mTabHost.newTabSpec("tab_gard")
.setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))
.setContent(R.id.gardlinarlayout));
mTabHost.addTab(mTabHost.newTabSpec("tab_test")
.setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))
.setContent(R.id.testlinarlayout));
mTabHost.addTab(mTabHost.newTabSpec("tab_set")
.setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))
.setContent(R.id.setlinarlayout));
//设置TabHost的背景颜色
mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
//设置TabHost的背景图片资源
mTabHost.setBackgroundResource(R.drawable.bg0);
//设置当前显示哪一个标签
mTabHost.setCurrentTab(0);
//标签切换事件处理,setOnTabChangedListener
/*mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
// TODO Auto-generated method stub
@Override
public void onTabChanged(String tabId)
{
Dialog dialog = new AlertDialog.Builder(EyesightMain.this)
.setTitle("提示")
.setMessage("当前选中:"+tabId+"标签")
.setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.cancel();
}
}).create();//创建按钮
dialog.show();
}
});*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
myMenu = menu;
myMenu.clear();//娓呯┖MENU鑿滃崟
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
//浠嶵abActivity杩欓噷鑾峰彇涓�釜MENU杩囨护鍣�
switch (myMenuSettingTag) {
case 1:
inflater.inflate(myMenuResources[0], menu);
//鍔ㄦ�鍔犲叆鏁扮粍涓搴旂殑XML MENU鑿滃崟
break;
case 2:
inflater.inflate(myMenuResources[1], menu);
break;
case 3:
inflater.inflate(myMenuResources[2], menu);
break;
default:
inflater.inflate(myMenuResources[0], menu);
break;
}
return super.onCreateOptionsMenu(menu);
} /*@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "即将转到另一个界面",
// Toast.LENGTH_LONG).show();
return super.onMenuItemSelected(featureId, item);
}*/ @Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
System.out.println(item.getItemId()); if (item.getItemId() == 0) {
System.out.println("intreasting");
} else if (item.getItemId()==1) {
System.out.println("intreasting");
} return super.onOptionsItemSelected(item);
} @Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
if (tabId.equals("tab_gard")) {
myMenuSettingTag = 1;
}
if (tabId.equals("tab_test")) {
myMenuSettingTag = 2;
}
if (tabId.equals("tab_set")) {
myMenuSettingTag = 3;
}
if (myMenu != null) {
onCreateOptionsMenu(myMenu);
}
}
}
打印出的getitemid值
是不是id不匹配呀
System.out.println("intreasting");
} else if (item.getItemId()==1) {
System.out.println("intreasting");
}
你的id值不匹配啊,所以没办法出发事件
<menu
xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/item_a"
android:icon="@drawable/gimp"
android:title="Gimp">
</item></menu>
这里我设置了啊,item android:id="@+id/item_a"