试试这个。这是您查询的完整答案。
还可以在 AndroidManifest 中授予 Internet 权限
private WebView wv1;
String url;
Spinner spinner;
private String[] spinner_and_webview = {"Learn Spinner", "Learn WebView"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter0 =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, spinner_and_webview);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter0);
spinner.setOnItemSelectedListener(onItemSelectedListener0);
wv1 = (WebView) findViewById(R.id.webView);
wv1.setWebViewClient(new MyBrowser());
}
AdapterView.OnItemSelectedListener onItemSelectedListener0 =
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String sp1 = String.valueOf(spinner.getSelectedItem());
if (sp1.contentEquals("Learn Spinner"))
url = "http://developer.android.com/guide/topics/ui/controls/spinner.html";
if (sp1.contentEquals("Learn WebView"))
url = "http://developer.android.com/reference/android/webkit/WebView.html";
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}