简体   繁体   English

实施 onClick on 子菜单以开始新活动

[英]Implement onClick on submenu to start new activity

I'm building an app which has menu item in toolbar, i have created submenu for one menu ie whose id is 'cloud'.我正在构建一个在工具栏中有菜单项的应用程序,我为一个菜单创建了子菜单,即其 ID 为“云”。 I would like to implement onclick on each submenu when click will open different activity.当单击将打开不同的活动时,我想在每个子菜单上实现 onclick。

Here is the menu.xml file这是 menu.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>

Here is the Mainactivity file这是 Mainactivity 文件

 @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); }

I looked for answer and tried on my own but I have no idea how to achieve this.我寻找答案并自己尝试,但我不知道如何实现这一目标。

Any help is appreciated.任何帮助表示赞赏。

Thank you in advance.先感谢您。

I have a solution for you:我有一个解决方案给你:

@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();
}

I write toast method to show a toast when users click on an item on menu or sub menu.我编写了toast方法来在用户单击菜单或子菜单上的项目时显示 toast。 Please remove //TODO: line and add your code for each case there.请删除//TODO:行并在那里为每个案例添加您的代码。

I don't think you want to have我认为你不想拥有

android:checkableBehavior="single"

set on your submenu.在您的子菜单上设置。 This behaviour is only useful if you just want to capture the choice (or choices) in the menu item, but not if you actually want to react (ie: do something) when the user selects a submenu item.此行为仅在您只想捕获菜单项中的选项(或多个选项)时有用,但如果您真的想在用户选择子菜单项时做出反应(即:做某事),则没有用。

Based on your question, it sounds like you want to launch another Activity when the user selects one of these submenu items.根据您的问题,听起来您想在用户选择这些子菜单项之一时启动另一个Activity To do that just add those menu items to the switch statement in onOptionsItemSelected() :为此,只需将这些菜单项添加到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;

Another interesting solution could be:另一个有趣的解决方案可能是:

@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);
}

That way you avoid having to have multiple times, in all cases the same call to the method.这样您就可以避免多次调用该方法,在所有情况下都必须进行相同的调用。

startActivity()

Saving lines of code.保存代码行。 It is even more advantageous if you decide to add more items to the menu.如果您决定向菜单添加更多项目,则更加有利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM