WebView - 查看2个Web

时间:2012-01-30 00:26:11

标签: android webview

我有这段代码:

package butterfly.menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MenuActivity extends Activity {
   WebView mWebView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if (savedInstanceState != null)
            ((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true); 
        mWebView.getSettings().setSupportZoom(false); 

        mWebView.loadUrl("http://myweb.pl/web2");
        mWebView.setWebViewClient(new MenuClient());
    }
    private class MenuClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    protected void onSaveInstanceState(Bundle outState) {
       mWebView.saveState(outState);
     }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_CALL || keyCode == KeyEvent.KEYCODE_POWER || keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_CLEAR || keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_POWER)) {
            //mWebView.goBack();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

}

此代码100%正确。我需要这样做: a)如果我的手机处于水平位置 - 然后加载:http://myweb.pl/web2/index1.html b)如果我的手机处于垂直位置,则加载:http://myweb.pl/web2/index2.html

谁知道我必须做什么?

感谢您的帮助


它没有做:(

我有这个:

package butterfly.menu;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

    public class MenuActivity extends Activity {
        WebView mWebView;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            if (savedInstanceState != null)
                ((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);

            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.getSettings().setJavaScriptEnabled(true); // włączenie JS
            mWebView.getSettings().setSupportZoom(false); // blokada ZOOMu

            mWebView.loadUrl("http://topvesta.home.pl/tablety"); // strona do wyświetlenia
            mWebView.setWebViewClient(new MenuClient());
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            {
                mWebView.loadUrl(" http://www.interia.pl");
            }

            else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                mWebView.loadUrl("http://wp.pl");
            }
        }
        private class MenuClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }
        protected void onSaveInstanceState(Bundle outState) {
            mWebView.saveState(outState);
         }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_CALL || keyCode == KeyEvent.KEYCODE_POWER || keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_CLEAR || keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_POWER)) {
                //mWebView.goBack();
                return false;
            }
            return super.onKeyDown(keyCode, event);
        }

    }

我一直都是onet.pl - 更新的wp.pl :(

1 个答案:

答案 0 :(得分:0)

使用

基于设备方向动态加载WebView
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        mWebView.loadUrl(" http://myweb.pl/web2/index2.html");
    }

    else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        mWebView.loadUrl(" http://myweb.pl/web2/index1.html");
    }