网页未显示在 webview 中并被重定向到浏览器
Webpage not being displayed in webview & getting redirected to browser
我正在尝试实现基本的网络视图。但是,当我输入 url 并单击按钮加载它时,我会弹出一个窗口,要求我从应用程序列表中进行选择以加载该 webpage.The 网页,然后在 [=14] 中完美加载=] 在清单文件中添加了 INTERNET 权限。请帮我找出错误或缺失的逻辑。
public class MainActivity extends Activity {
private static final String TAG = "WebViewActivity";
Button button1;
EditText et1;
String address;
WebView wv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
wv1=(WebView)findViewById(R.id.webView1);
wv1.getSettings().setJavaScriptEnabled(true);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
address=et1.getText().toString();
Log.d(TAG,"url has been stored in address");
wv1.loadUrl(address);
Log.d(TAG,"url should now be loaded in webview");
}
});
}
请设置 setWebViewClient 侦听器:
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
view.loadUrl(url);
return false;
}
});
现在用您的代码替换此代码...
public class MainActivity extends Activity {
private static final String TAG = "WebViewActivity";
Button button1;
EditText et1;
String address;
WebView wv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
wv1=(WebView)findViewById(R.id.webView1);
wv1.setWebViewClient(new MyBrowser());
wv1.getSettings().setJavaScriptEnabled(true);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
address=et1.getText().toString();
Log.d(TAG,"url has been stored in address");
wv1.loadUrl(address);
Log.d(TAG,"url should now be loaded in webview");
}
});
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
}
}
我正在尝试实现基本的网络视图。但是,当我输入 url 并单击按钮加载它时,我会弹出一个窗口,要求我从应用程序列表中进行选择以加载该 webpage.The 网页,然后在 [=14] 中完美加载=] 在清单文件中添加了 INTERNET 权限。请帮我找出错误或缺失的逻辑。
public class MainActivity extends Activity {
private static final String TAG = "WebViewActivity";
Button button1;
EditText et1;
String address;
WebView wv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
wv1=(WebView)findViewById(R.id.webView1);
wv1.getSettings().setJavaScriptEnabled(true);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
address=et1.getText().toString();
Log.d(TAG,"url has been stored in address");
wv1.loadUrl(address);
Log.d(TAG,"url should now be loaded in webview");
}
});
}
请设置 setWebViewClient 侦听器:
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
view.loadUrl(url);
return false;
}
});
现在用您的代码替换此代码...
public class MainActivity extends Activity {
private static final String TAG = "WebViewActivity";
Button button1;
EditText et1;
String address;
WebView wv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
wv1=(WebView)findViewById(R.id.webView1);
wv1.setWebViewClient(new MyBrowser());
wv1.getSettings().setJavaScriptEnabled(true);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
address=et1.getText().toString();
Log.d(TAG,"url has been stored in address");
wv1.loadUrl(address);
Log.d(TAG,"url should now be loaded in webview");
}
});
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
}
}