实现单击子菜单以开始新活动



我正在构建一个在工具栏中具有菜单项的应用程序,我已经为一个菜单创建了子菜单,即其ID为"云"。我想在每个子菜单上实现onclick,当点击将打开不同的活动时。

这是菜单.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_refresh"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="@drawable/ic_loop_black_24dp"></item>
<item
android:id="@+id/notes"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="@drawable/ic_event_note_black_24dp"></item>
<item
android:id="@+id/cloud"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="@drawable/ic_cloud_upload_blackt_24dp">
<menu>
<group
android:checkableBehavior="single">
<item android:id="@+id/imageee"
android:title="Image Upload"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="@+id/pdfff"
android:title="Pdf Upload"
android:orderInCategory="100"
app:showAsAction="never"/>
</group>
</menu>
</item>
</menu>

这是主活动文件

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
mymenu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_refresh:
Intent intent = new Intent(MainActivity.this, UpdateService.class);
startService(intent);

}
return true;
case R.id.notes:{
Intent activity_weather = new Intent(this,Physics.class);
startActivity(activity_weather);
}
return true;
case R.id.cloud:{
}
return true;
}
return super.onOptionsItemSelected(item);
}

我寻找答案并自己尝试,但我不知道如何实现这一目标。

任何帮助,不胜感激。

提前谢谢你。

我有一个解决方案给你:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
// TODO:
toast("refresh");
return true;
case R.id.notes:
// TODO:
toast("notes");
return true;
case R.id.imageee:
// TODO: Start your image upload
toast("imageee");
return true;
case R.id.pdfff:
// TODO: Start your PDF upload
toast("pdfff");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void toast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

我编写toast方法,当用户单击菜单或子菜单上的项目时显示 Toast。请删除//TODO:行并在此处为每个案例添加代码。

我认为你不想有

android:checkableBehavior="single"

设置在您的子菜单上。仅当您只想捕获菜单项中的一个或多个选项时,此行为才有用,但如果您实际上想要在用户选择子菜单项时做出反应(即:执行某些操作(,则此行为没有用。

根据您的问题,当用户选择其中一个子菜单项时,听起来您想启动另一个Activity。为此,只需将这些菜单项添加到onOptionsItemSelected()中的switch语句中:

case R.id.imageee:
Intent imageIntent = new Intent(this, Upload.class);
startActivity(imageIntent);
return true;
case R.id.pdfff:
Intent pdfIntent = new Intent(this, Pdf.class);
startActivity(pdfIntent);
return true;

另一个有趣的解决方案可能是:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case R.id.action_refresh:
intent.setClass(this, UpdateService.class);
break;
case R.id.notes:
intent.setClass(this, Physics.class);
break;
case R.id.cloud:
intent = null;           
break
case R.id.imageee:
// TODO set intent class
break;
case R.id.pdfff:
// TODO set intent class
break;
}
// If the selected item is the one that opens the submenu does not try to start the 
// activity avoiding throwing null pointer exception and consequently opens the submenu
if (!(intent == null))  
startActivity(intent);
return super.onOptionsItemSelected(item);
}

这样,您就不必在所有情况下多次调用该方法。

startActivity()

保存代码行。如果您决定向菜单添加更多项目,则更有优势。

最新更新