我想等到这样的目的: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);
}
}
}
解决方案 »
- adb shell里面ls提示opendir failed, Permission denied
- ????qiu jieshi
- 间接Intent与直接Intent相比,各有什么优点
- LBS定位
- 如果我有个anroid手机,我能不能在上面安装一些软件,在手机上写程序
- 求大神,已经屏蔽了第三方库的代码混淆,为什么还会混淆
- android调用webservice连接oracle出错,求助!
- android开发环境搭建的问题咨询
- 我现在做android xmpp聊天室,邀请人的时候,被邀请的人监听不到,求大神们指点
- android webview通过js来调用本地播放器,传参为视频地址
- 类似于某些google的东西
- 为何设置padding数值之后,背景色还是会填充了整个屏幕?
打印出的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"