我正在尝试实现基本的webview。但是,当我输入网址并点击按钮加载它时,我会弹出一个弹出窗口,要求我从应用程序列表中选择加载该网页。然后网页在浏览器中完美加载。我已添加了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");
}
});
}
答案 0 :(得分:0)
请设置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;
}
});
答案 1 :(得分:0)
现在用您的代码替换此代码......
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) {
}
}