为了帮助您更好地理解,我将简单的浏览器应用代码留给您。
这里 SimpleBrowser 是 MainActivity。
public class SimpleBrowser extends Activity implements OnClickListener {
WebView ourBrowser;
EditText url;
Button go, go_Back, go_Forward, refresh, clr_History;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_webview);
ourBrowser = (WebView) findViewById(R.id.WVBrowser);
/* WebView Settings pretty important */
ourBrowser.getSettings().setJavaScriptEnabled(true);
ourBrowser.getSettings().setLoadWithOverviewMode(true);
ourBrowser.getSettings().setUseWideViewPort(true);
ourBrowser.setWebViewClient(new ourViewClient());
try {
ourBrowser.loadUrl("http://www.mybringback.com");
} catch (Exception e) {
e.printStackTrace();
}
go = (Button) findViewById(R.id.btn_Go);
go_Back = (Button) findViewById(R.id.btn_GoBack);
go_Forward = (Button) findViewById(R.id.btn_GoForward);
refresh = (Button) findViewById(R.id.btn_RefreshPage);
clr_History = (Button) findViewById(R.id.btn_ClrHistory);
url = (EditText) findViewById(R.id.eT_webbrowser);
go.setOnClickListener(this);
go_Back.setOnClickListener(this);
go_Forward.setOnClickListener(this);
refresh.setOnClickListener(this);
clr_History.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_Go:
String newWebaddress = url.getText().toString();
ourBrowser.loadUrl(newWebaddress);
/* Hiding the keyboard after the EditText data */
InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);
break;
case R.id.btn_GoBack:
if (ourBrowser.canGoBack()) {
ourBrowser.goBack();
}
break;
case R.id.btn_GoForward:
if (ourBrowser.canGoForward()) {
ourBrowser.goForward();
}
break;
case R.id.btn_RefreshPage:
ourBrowser.reload();
break;
case R.id.btn_ClrHistory:
ourBrowser.clearHistory();
break;
}
}
}
然后有另一个名称为:ourViewClient.java 的 Java 文件,它应该包含以下代码:
public class ourViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true; // as mentioned in below notes, for your case., you do 'return false'
}
}
Android 清单文件:
确保声明了<uses-permission android:name="android.permission.INTERNET" />。
browser_webview.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/eT_webbrowser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_Go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Go" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_GoBack"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Go back a Page"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_GoForward"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Go Forward"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_RefreshPage"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_weight="0.90"
android:text="Refresh Page"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_ClrHistory"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:text="Clear History"
android:textSize="@dimen/mediumsize" />
</LinearLayout>
<WebView
android:id="@+id/WVBrowser"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
</LinearLayout>