按“设置”(或其他按钮)时查看另一个webview网页

时间:2015-08-17 15:46:42

标签: android webview

我正在构建我的第一个(android)应用程序,我将它基于webview,因为我在PHP中更强大。我创建了该页面,并且app / webpage运行良好。但为了让事情发挥作用,我在网页上创建了自己的按钮(例如,关于应用程序)。但是在Android Studio中使用菜单功能会更好。 F.ex.添加:<item android:id="@+id/action_settings"我现在有了按钮,但我不知道如何将其链接到关于页面。

更新1: WebviewActivity.java的源代码:

package com.***.***;

import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebviewActivity extends Activity {

    private static final String HTTP_URL = "***index***";

    private static final String HTTP_URL_GRAPH = "***index***";

    private static final String TAG = WebviewActivity.class.getSimpleName();

    private WebView webView;

    static Dialog mDialog;

    private static final int ACTIVATE = 0;

    private static final long ACTIVATE_DELAY = 400;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_activity);
        Log.d(TAG, "Starting WebActivity ...");

        assignViews();
        assignCallbacks();
        createWebView();
    }

    @Override
    public void onBackPressed(){
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_graph) {
            createWebViewGraph();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // handler for activation of WebView (kind of loading)
    private static Handler webviewActivatorHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ACTIVATE:
                    if (mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
            }
            super.handleMessage(msg);
        }
    };

    @SuppressLint("SetJavaScriptEnabled")
    private void createWebView() {
        webView = (WebView) findViewById(R.id.webView1);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);

        // loading dialog
        mDialog = new Dialog(this, android.R.style.Theme_Light);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(R.layout.webview_overlay);
        mDialog.show();

        webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Message msg = new Message();
                msg.what = ACTIVATE;
                webviewActivatorHandler.sendMessageDelayed(msg, ACTIVATE_DELAY);
                updateUI();
            }

        });

        if (DeviceHelper.isOnline(this)) {
            webView.loadUrl(HTTP_URL);
        } else {
            DialogHelper.displayCustomDialog(this, getString(R.string.offline),
                    getString(R.string.offline_msg));
            try {
                mDialog.dismiss();
            } catch (Exception e) {
                Log.e(TAG, "Error dismissing dialog");
            }
        }
    }

    private void createWebViewGraph() {
        webView = (WebView) findViewById(R.id.webView1);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);

        // loading dialog
        mDialog = new Dialog(this, android.R.style.Theme_Light);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(R.layout.webview_overlay);
        mDialog.show();

        webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Message msg = new Message();
                msg.what = ACTIVATE;
                webviewActivatorHandler.sendMessageDelayed(msg, ACTIVATE_DELAY);
                updateUI();
            }

        });

        if (DeviceHelper.isOnline(this)) {
            webView.loadUrl(HTTP_URL_GRAPH);
        } else {
            DialogHelper.displayCustomDialog(this, getString(R.string.offline),
                    getString(R.string.offline_msg));
            try {
                mDialog.dismiss();
            } catch (Exception e) {
                Log.e(TAG, "Error dismissing dialog");
            }
        }
    }

    private void updateUI() {
        // update UI elements here
    }

    private void assignCallbacks() {
        // assign callbacks here
    }

    private void assignViews() {
        // assign views here
    }

}

0 个答案:

没有答案