问题描述:

I am displaying webpages in a webview in my app. I want to enable screen orientation only when user clicks/touch on an edittext inside webview.I have tried below code.

 mWebView.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

WebView.HitTestResult hr = ((WebView)v).getHitTestResult();

if(hr!=null){

if(hr.getType()==HitTestResult.EDIT_TEXT_TYPE)

((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

else

((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

return false;

}

});

But it is not working in OS versions below 2.3. How can i do this in these versions also.please help me .i am stuck on this..

网友答案:

below code working for edittext click inside webview :

WebView web_view = (WebView) subview
                    .findViewById(R.id.webview_mytagdetail_text);

web_view.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    Intent in = new Intent(getActivity(), URL_SCREEN.class);
                    in.putExtra("URL", url);
                    startActivity(in);

                    return true;

                }
            });

            web_view.getSettings().setJavaScriptEnabled(true);
            web_view.getSettings().setLoadWithOverviewMode(true);
            web_view.getSettings().setBuiltInZoomControls(true);
            web_view.getSettings().setDisplayZoomControls(false);

            web_view.loadDataWithBaseURL("", campaignClass.getCampaignDesc(),
                    "text/html", "UTF-8", "");
相关阅读:
Top