我有一个webview并加载它的网页。工作得很好。
但问题是,"当我点击从网页重定向到浏览器的任何链接而不是在创建的网页中#34;。
我想在同一个网页上打开它。
答案 0 :(得分:0)
如果您想要更好地控制点击链接的加载位置,请创建一个覆盖 shouldOverrideUrlLoading()方法的WebViewClient。
在webview中的loadUrl()之前添加以下代码。
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
更多详情请参阅Web Apps in WebView
答案 1 :(得分:0)
创建一个扩展WebViewClient的类:
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
view.loadUrl(urlNewString);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
}
}
然后将webViewClient设置为您的webview最初:
webView.setWebViewClient(new MyBrowser());
答案 2 :(得分:0)
在特定活动代码
中的AndroidMenifest.xml
中使用以下代码
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
答案 3 :(得分:0)
这里我有一个示例Activity类。
每当您尝试在WebView中打开链接时,浏览器都会打开该链接,因为您的代码中没有使用任何WebViewClient 。请参考我与您分享的以下代码!
public class WebViewClientDemoActivity extends Activity {
/** Called when the activity is first created. */
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
// Setting the WebView Client for the WebView over here
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.google.com");
}
// The WebViewClient class
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
如果有效,请告诉我! :)
答案 4 :(得分:0)
只需在代码中添加以下行
即可private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
并为您的webview设置上面的webview客户端,如下所示:
webView.setWebViewClient(new MyWebViewClient());
答案 5 :(得分:0)
试试这个:
public class MainActivity extends Activity {
Button b1;
EditText ed1;
private WebView wv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);
wv1=(WebView)findViewById(R.id.webView);
wv1.setWebViewClient(new MyBrowser());
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = ed1.getText().toString();
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
}
});
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
如需进一步参考,请访问以下链接
http://www.tutorialspoint.com/android/android_webview_layout.htm