2

我有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;我在相关类中导入。

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

任何帮助将不胜感激!

4

3 回答 3

0
 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);
        }
    });

尝试这个

于 2013-11-05T07:28:36.930 回答
0

做这样的事情

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>
于 2013-11-05T12:06:18.493 回答
0

请定义和导入包

    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();

在 android webview 中使用 javascript访问 http://developer.android.com/guide/webapps/webview.html

于 2013-11-11T07:51:43.000 回答