【问题标题】:Android/WebView - Change OrientationAndroid/WebView - 改变方向
【发布时间】:2013-11-05 06:50:31
【问题描述】:

我在一个活动中有一个webView。在里面,webView 我有一个按钮,可以打开一个新的HTML 页面。当按下此按钮并打开新的 html 页面时,我希望屏幕方向更改为水平。

var searchButton = $('<button/>',
            {
                text:'Search!',
                "class":"buttons",
                id: "searchButtonID",
                click: function(){

                var select = document.getElementById("subCategory");
                var option = select.options[select.selectedIndex];
                var subCategoryId = option.id;
                window.location.href = "deals.html?subCategoryId=" + subCategoryId; 

                Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);             
            }
        });

我最近了解到WebAppInterfaces 允许 Javascript 与 Android 交互。我尝试添加该行:

Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

但是,我收到一条错误消息:

 Uncaught ReferenceError: ActivityInfo is not defined

我在相关类中导入了import android.content.pm.ActivityInfo;。

不确定我正在做的事情是否可能......

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript android webview screen-orientation


    【解决方案1】:
     private void rotate(float degree) {
        final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    
        rotateAnim.setDuration(0);
        rotateAnim.setFillAfter(true);
        oWebView.startAnimation(rotateAnim);
    }
    
    btn.setOnClickListener(new OnClickListener()
        {
    
            @Override
            public void onClick(View v)
            {
    
    
                rotate(90);
            }
        });
    

    试试这个

    【讨论】:

    • Nithinlal 你能解释一下这段代码如何通过“Javascript”代码工作到“setOnClickListener”吗?谢谢。
    • 嗨 Nithinlal,我应该访问 'click: function(){}' 中的“旋转”功能,而不是使用 btn.setOnClickListener(new OnClickListener()。其次,我如何访问我的webview,因为它与我的WebAppInterface 在不同的类中?
    【解决方案2】:

    做这样的事情

    boolean rotationRequired = false;    
    
    private void openWebviewDialog(String days)
    {
    dialog = new Dialog(MainActivity.GLOBAL_CONTEXT);
    dialog.setContentView(R.layout.simple_webview);
    WebView wb = (WebView) dialog.findViewById(R.id.simple_webview);
    WebSettings webSettings = wb.getSettings(); 
    wb.setWebViewClient(new MyWebViewClient());
    webSettings.setJavaScriptEnabled(true);
    wb.addJavascriptInterface(this, "javaScriptInterface");
    String url = "http://www.yourdomain.com" // your url here
    String title = "Hello";
    
    wb.loadUrl(url);
    dialog.setCancelable(true);
    dialog.setTitle(title);
    dialog.show();
    }
    
    class MyWebViewClient extends WebViewClient 
    {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    {
        if(url.contains("openExternal"))
        { 
            return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
        } 
        else 
        {
            view.loadUrl(url); // Stay within this webview and load url
            return true;
        }
    }
    
    @Override
    public void onPageFinished(WebView view, String url) 
    {
        super.onPageFinished(view, url);
        if(rotationRequired)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }
    
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) 
    {
        super.onPageStarted(view, url, favicon);
    }
    }
    
    @JavascriptInterface
    public void passResult(String status) 
    {
       rotationRequired = status.equalsIgnoreCase("true") ? true : false;
    }
    

    不要忘记将其添加到您的清单中:

    android:configChanges = "orientation"
    

    控制旋转为

     <p>rotate...</p><script type='text/javascript' language='javascript'>javaScriptInterface.passResult('true');</script>
    

    【讨论】:

      【解决方案3】:

      请定义并导入包

          WebView myWebView;
          WebAppInterface mWebAppInterface;
      

      将此代码添加到活动的 onCreate() 中

      mWebAppInterface = new WebAppInterface(this);
      myWebView = (WebView) findViewById(R.id.webview);
      myWebView.addJavascriptInterface(mWebAppInterface, "Android1");
      WebSettings webSettings = myWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      //load your URL by myWebView.loadUrl("www.example.com");
      

      也添加这个公共函数

      @JavascriptInterface
      public void setorientation(){
      
           setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      }
      

      现在你可以调用这个公共函数来从javascript按钮点击函数设置方向

      Android1.setorientation();
      

      也请访问 http://developer.android.com/guide/webapps/webview.html using javascript in android webview

      【讨论】:

        猜你喜欢
        • 2010-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多