Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 对整个应用程序中的所有选项菜单使用相同的方法_Android_Onclick_Android Optionsmenu - Fatal编程技术网

Android 对整个应用程序中的所有选项菜单使用相同的方法

Android 对整个应用程序中的所有选项菜单使用相同的方法,android,onclick,android-optionsmenu,Android,Onclick,Android Optionsmenu,我有10到12个活动,所有活动都有帮助菜单作为选项菜单。 我成功地使用以下代码创建它,并在单击它们时显示帮助 Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(cacheDir, "HELP.pdf

我有10到12个
活动
,所有
活动
都有
帮助菜单
作为
选项菜单
。 我成功地使用以下代码创建它,并在单击它们时显示帮助

    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(cacheDir, "HELP.pdf")),"application/pdf");

    context.startActivity(intent);
但我想减少所有活动的代码,为此我创建了一个类并生成了一个方法,但我仍然想减少代码

我搜索并发现
onClick
属性在
OptionMenu
中可用,但我不知道如何使用它


请帮助..

创建一个类,例如调用它
Helper
,在其中放置一个名为
handleMenu(int-id)
的方法,并在其中执行所有工作。然后,在每个活动中,您从onOptionsItemSelected()调用该方法,传递所选项目的id。

我为openFile创建了以下类:

public class OpenHelpFile {

    File cacheDir;
    Context context;

    /* Constructor */
    public OpenHelpFile(Context context) {
        this.context = context;

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "OOPS");
        else
            cacheDir = context.getCacheDir();

        if (!cacheDir.exists())
            cacheDir.mkdirs();

        try {
            File helpFile = new File(cacheDir, "OOPS.pdf");

            if (!helpFile.exists()) {
                InputStream in = context.getAssets().open("OOPS.pdf");
                OutputStream out = new FileOutputStream(helpFile);

                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();

                Log.d(TAG, "File Copied...");
            }
            Log.d(TAG, "File exist...");

        } catch (FileNotFoundException ex) {
            Log.d(TAG, ex.getMessage() + " in the specified directory.");
            System.exit(0);
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
        }
    }

    public void openHelpFile() {

        /* OPEN PDF File in Viewer */
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(cacheDir, "OOPS.pdf")), "application/pdf");

        context.startActivity(intent);
    }
}
我从每个
选项菜单中调用它,如下所示:

new OpenHelpFile(context).openHelpFile();

当你按下“选项”菜单按钮时会触发PDF文档?是的,绝对正确。创建一个包含所有活动的超类,并将代码实现到该类中,但有一些活动和片段正在扩展其他类。您的其他
活动将扩展
活动
,因此您
在超类中扩展活动
,并
扩展基本活动
(或您想叫它什么)在您的其他
活动中
需要什么
int id
,这样您就可以知道菜单的哪个项目被按下了,如xml菜单文件中定义的那样。在任何帮助菜单中,我都必须按如下方式调用