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

このようなことをしてください

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
 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 に答える