从 MenuItem 获取 ID 并转到下一个 Activity
Get Id from MenuItem and Go To Next Activity
我创建了一个 Menu
和 4 menuitems
。我需要在成为 pressed
时检索 menuitems
ID,然后必须进行检查以查看是什么Id 已经 pressed
并转到新的 Activity
。我试过使用 item.getTitle()
和 item.getItemId()
但它没有进入 if statement
。
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/one"
android:title="TOOL 7 - THE FORCE"/>
<item
android:id="@+id/two"
android:title="TOOL 5 - FOCUS"/>
<item
android:id="@+id/three"
android:title="TOOL 3 - ACTION"/>
<item
android:id="@+id/four"
android:title="TOOL 9 - QUESTION FIX"/>
</menu>
检索 MenuItemId 的代码
btnToolbox = (Button) findViewById(R.id.btnToolbox);
btnToolbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(Screen2_11.this,btnToolbox);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
itemName = String.valueOf(item.getTitle());
itemId = item.getItemId();
if ( itemName == "TOOL 7 - THE FORCE" || itemId == "one" ){
Intent i = new Intent(getApplicationContext(), Screen108_Force.class);
startActivity(i);
}
else if ( itemName == "TOOL 5 - FOCUS" || itemId == "two" ) {
Intent i = new Intent(getApplicationContext(), Screen120_Focus.class);
startActivity(i);
}
else if ( itemName == "TOOL 3 - ACTION" || itemId == "three" ) {
Intent i = new Intent(getApplicationContext(), Screen119_Action.class);
startActivity(i);
}
else if ( itemName == "TOOL 9 - QUESTION FIX" || itemId == "four" ) {
Intent i = new Intent(getApplicationContext(), Screen136_Questions.class);
startActivity(i);
}
return true;
}
});
popup.show();
}
});
使用这个 if (itemId == R.id.one)
而不是 if ( itemName == "TOOL 7 - THE FORCE" || itemId == "one" )
试试这个
btnToolbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(Screen2_11.this, btnToolbox);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.one) {
Intent i = new Intent(getApplicationContext(), Screen108_Force.class);
startActivity(i);
} else if (itemId == R.id.two) {
Intent i = new Intent(getApplicationContext(), Screen120_Focus.class);
startActivity(i);
} else if (itemId == R.id.three) {
Intent i = new Intent(getApplicationContext(), Screen119_Action.class);
startActivity(i);
} else if (itemId == R.id.four) {
Intent i = new Intent(getApplicationContext(), Screen136_Questions.class);
startActivity(i);
}
return true;
}
});
popup.show();
}
});
您需要先初始化菜单。完成 onCreate()
后添加这部分
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main,menu);
return true;
}
然后在此处处理项目按下的选项。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//here initialize your button pressed.
return super.onOptionsItemSelected(item);
}
我创建了一个 Menu
和 4 menuitems
。我需要在成为 pressed
时检索 menuitems
ID,然后必须进行检查以查看是什么Id 已经 pressed
并转到新的 Activity
。我试过使用 item.getTitle()
和 item.getItemId()
但它没有进入 if statement
。
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/one"
android:title="TOOL 7 - THE FORCE"/>
<item
android:id="@+id/two"
android:title="TOOL 5 - FOCUS"/>
<item
android:id="@+id/three"
android:title="TOOL 3 - ACTION"/>
<item
android:id="@+id/four"
android:title="TOOL 9 - QUESTION FIX"/>
</menu>
检索 MenuItemId 的代码
btnToolbox = (Button) findViewById(R.id.btnToolbox);
btnToolbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(Screen2_11.this,btnToolbox);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
itemName = String.valueOf(item.getTitle());
itemId = item.getItemId();
if ( itemName == "TOOL 7 - THE FORCE" || itemId == "one" ){
Intent i = new Intent(getApplicationContext(), Screen108_Force.class);
startActivity(i);
}
else if ( itemName == "TOOL 5 - FOCUS" || itemId == "two" ) {
Intent i = new Intent(getApplicationContext(), Screen120_Focus.class);
startActivity(i);
}
else if ( itemName == "TOOL 3 - ACTION" || itemId == "three" ) {
Intent i = new Intent(getApplicationContext(), Screen119_Action.class);
startActivity(i);
}
else if ( itemName == "TOOL 9 - QUESTION FIX" || itemId == "four" ) {
Intent i = new Intent(getApplicationContext(), Screen136_Questions.class);
startActivity(i);
}
return true;
}
});
popup.show();
}
});
使用这个 if (itemId == R.id.one)
而不是 if ( itemName == "TOOL 7 - THE FORCE" || itemId == "one" )
试试这个
btnToolbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(Screen2_11.this, btnToolbox);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.one) {
Intent i = new Intent(getApplicationContext(), Screen108_Force.class);
startActivity(i);
} else if (itemId == R.id.two) {
Intent i = new Intent(getApplicationContext(), Screen120_Focus.class);
startActivity(i);
} else if (itemId == R.id.three) {
Intent i = new Intent(getApplicationContext(), Screen119_Action.class);
startActivity(i);
} else if (itemId == R.id.four) {
Intent i = new Intent(getApplicationContext(), Screen136_Questions.class);
startActivity(i);
}
return true;
}
});
popup.show();
}
});
您需要先初始化菜单。完成 onCreate()
后添加这部分@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main,menu);
return true;
}
然后在此处处理项目按下的选项。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//here initialize your button pressed.
return super.onOptionsItemSelected(item);
}