-2

我有 10 到 12 个,Activity全部Activity都有。我成功地使用了以下代码来创建它并在点击它们时显示帮助。Help MenuOption Menu

    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但我不知道如何使用它。

请帮忙..

4

2 回答 2

0

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

于 2013-12-22T16:29:18.273 回答
0

我为 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);
    }
}

我已经这样称呼它OptionMenu

new OpenHelpFile(context).openHelpFile();
于 2013-12-23T05:29:54.567 回答