如何在网络视图中创建后退按钮?

发布于 2025-01-08 08:53:11 字数 2528 浏览 3 评论 0原文

该应用程序由两个活动组成:第一个 - 一个列表视图,其中包含资产中 HTML 文件的链接;第二个 - 一个包含此 HTML 文件的 Web 视图。

因此,此 HTML 文件(在下文中我将其称为第一个 HTML 文件)中有一些链接指向资产中的其他 HTML 文件。在我点击其中一个链接后,我想返回到第一个 HTML 文件。但这种方法显示的是白屏而不是文件的内容。

PS:我通过 HTML 链接引用第一个资源文件中的第二个资源文件。例如(第一个 HTML 文件中的链接代码):

<a href="file:///android_asset/second.html">Second file</a>

这是 WebViewActivity 代码:

public class WebViewActivity extends Activity implements OnClickListener {
    WebView wWebView;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (wWebView.canGoBack() == true) {
                    wWebView.goBack();
                } else {
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bundle bundle = getIntent().getExtras();

        String htmlFileName = "m" + bundle.getString("defStrID") + ".html";
        Context context = getBaseContext();

        try {
            String text = readAssetTextFile(context, htmlFileName);

            wWebView = (WebView) findViewById(R.id.webview);
            String summary = "<html><body>" + text + "</body></html>";
            wWebView.getSettings().setJavaScriptEnabled(true);
            wWebView.loadDataWithBaseURL("x-data://base", summary, "text/html",
                    "utf-8", null);
        } catch (IOException e) {
            Log.e("TAG", "Exception thrown", e);
        }
    }

    public static String readAssetTextFile(Context ctx, String fileName)
            throws IOException
    {
        InputStream inputStream = ctx.getAssets().open(fileName);

        InputStreamReader inputreader = new InputStreamReader(inputStream);
        BufferedReader buffreader = new BufferedReader(inputreader);
        String line;
        StringBuilder text = new StringBuilder();

        try {
            while ((line = buffreader.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        } catch (IOException e) {
            Log.e("TAG", "Exception thrown", e);
        }
        return text.toString();
    }
}

The app consists of two activities: first - a listview with links to HTML files from assets, second - a webview with this HTML file.

So there are some links in this HTML file (in the following I call it the first HTML file) leading to other HTML files from assets. After I followed one of this links I want to go back to the first HTML file. But this method shows me a white screen instead of the file's content.

PS: I reference the second asset file from first one by an HTML link. For example (code of link in the first HTML file):

<a href="file:///android_asset/second.html">Second file</a>

Here is the WebViewActivity code:

public class WebViewActivity extends Activity implements OnClickListener {
    WebView wWebView;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (wWebView.canGoBack() == true) {
                    wWebView.goBack();
                } else {
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bundle bundle = getIntent().getExtras();

        String htmlFileName = "m" + bundle.getString("defStrID") + ".html";
        Context context = getBaseContext();

        try {
            String text = readAssetTextFile(context, htmlFileName);

            wWebView = (WebView) findViewById(R.id.webview);
            String summary = "<html><body>" + text + "</body></html>";
            wWebView.getSettings().setJavaScriptEnabled(true);
            wWebView.loadDataWithBaseURL("x-data://base", summary, "text/html",
                    "utf-8", null);
        } catch (IOException e) {
            Log.e("TAG", "Exception thrown", e);
        }
    }

    public static String readAssetTextFile(Context ctx, String fileName)
            throws IOException
    {
        InputStream inputStream = ctx.getAssets().open(fileName);

        InputStreamReader inputreader = new InputStreamReader(inputStream);
        BufferedReader buffreader = new BufferedReader(inputreader);
        String line;
        StringBuilder text = new StringBuilder();

        try {
            while ((line = buffreader.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        } catch (IOException e) {
            Log.e("TAG", "Exception thrown", e);
        }
        return text.toString();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2025-01-15 08:53:11

我使用后退按钮,如下所示:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

这种方法的优点是它不会占用任何屏幕,

I use the Back button, as follows:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

The advantage of this approach is that it does not use up any of the screen,

网名女生简单气质 2025-01-15 08:53:11

您的问题是 wWebView 类成员变量未初始化,因此它为 null。看看你的代码的这一部分:

WebView wWebView = (WebView) findViewById(R.id.webview);

完美,你正在初始化,但不是你想象的那样:你为该方法声明了一个局部变量并初始化它。然而,由于局部变量具有相同的名称,因此您隐藏了类成员wWebView。因此,您不会初始化类成员,它为 null,并且您会得到 NPE。将上面的行更改为(注意缺少类型):

wWebView = (WebView) findViewById(R.id.webview);

编辑 我始终无法使您在页面之间导航的方式正常工作。不过,我对您的 onCreate 方法进行了某些更改,现在我的设备上一切看起来都很好:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    TextView title = (TextView) findViewById(R.id.app_name);
    title.setText(getString(R.string.app_name));

    Button backButton = (Button) findViewById(R.id.button_back);
    backButton.setOnClickListener(this);

    Button infoButton = (Button) findViewById(R.id.button_info);
    infoButton.setOnClickListener(this);

    Bundle bundle = getIntent().getExtras();

    String htmlFileName = "m" + bundle.getString("defStrID") + ".html";
    String LINK_TO_ASSETS = "file:///android_asset/";
    wWebView = (WebView) findViewById(R.id.webview);
    wWebView.getSettings().setJavaScriptEnabled(true);
    wWebView.loadURL(LINK_TO_ASSETS + htmlFileName);
}

现在您还需要更改加载的 HTML 文件的内容。首先,在资源中每个文件的开头添加 ,在末尾添加 。其次,现在您需要更改链接。例如,您在问题中包含的链接:

<a href="second.html">Second file</a>

如您所见,这些现在变成了相对链接。所有这些更改可能会使您的 readAssetTextFile 无用,但我相信这是更容易理解的代码。

Your problem is that the wWebView class member variable is not initialized so it is null. See this part of your code:

WebView wWebView = (WebView) findViewById(R.id.webview);

Perfect, you are initializing, but not what you think you are: you declare a local variable for the method and initialize it. However you shadow the class member wWebView, because of the local variable with the same name. Thus you do not initialize the class member, it is null and you get your NPE. Change the above line to (note the absence of the type):

wWebView = (WebView) findViewById(R.id.webview);

EDIT I was never able to make your way of navigating between the pages work. However I did certain changes to your onCreate method and now everything seems fine on my device:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    TextView title = (TextView) findViewById(R.id.app_name);
    title.setText(getString(R.string.app_name));

    Button backButton = (Button) findViewById(R.id.button_back);
    backButton.setOnClickListener(this);

    Button infoButton = (Button) findViewById(R.id.button_info);
    infoButton.setOnClickListener(this);

    Bundle bundle = getIntent().getExtras();

    String htmlFileName = "m" + bundle.getString("defStrID") + ".html";
    String LINK_TO_ASSETS = "file:///android_asset/";
    wWebView = (WebView) findViewById(R.id.webview);
    wWebView.getSettings().setJavaScriptEnabled(true);
    wWebView.loadURL(LINK_TO_ASSETS + htmlFileName);
}

Now you need to change also the contents of the HTML files you load. First of all add the <html><body> at the beginning of each file in the assets and </body></html> at the end. Second now you will need to change your links. For example the link you have included in your question:

<a href="second.html">Second file</a>

As you see, these become now relative links. All these changes might make your readAssetTextFile useless, but I believe this is easier-to-understand code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文