Android WebView WebViewClient
我的应用程序中有一个 webView。当一个人加载 URL 时,网页会加载到浏览器中,而不是加载到我的应用程序中,因为选项菜单是默认菜单,而不是我分配的菜单。我怎样才能阻止这个并让它加载到我的网络视图而不是浏览器中?
我尝试了 webViewClient 但它似乎不起作用。
public class webView extends Activity {
WebView myWebView;
String url;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
url = "http://d.android.com";
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//url="http://google.com";
//view.loadUrl(url);
System.out.println("hello");
return true;
}
});
//Toast.makeText(this, "", Toast.LENGTH_SHORT);
myWebView.loadUrl(url);
}
/** Creteing an options menu**/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//return true;
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
I have a webView in my application. When a person loads a url, the webpage is loaded in the browser and not in my application as the options menu is the default and not what I have assigned. How can I stop this and make it load in my webview and not the browser?
I tried webViewClient but it doesn't seem to work.
public class webView extends Activity {
WebView myWebView;
String url;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
url = "http://d.android.com";
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//url="http://google.com";
//view.loadUrl(url);
System.out.println("hello");
return true;
}
});
//Toast.makeText(this, "", Toast.LENGTH_SHORT);
myWebView.loadUrl(url);
}
/** Creteing an options menu**/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//return true;
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想在浏览器中打开 URL,则必须在
shouldOverrideUrlLoading
中返回 false
If you never want to open an URL in a browser you have to
return false
inshouldOverrideUrlLoading
另一种解决方案可能是:
默认情况下,默认 Web 浏览器打开并加载目标 URL,要覆盖此行为,请调用
another solution could be :
by default the default web browser opens and loads the destination URL, to overwrite this behavior, call