我正在尝试将webview后退按钮实现到我的应用程序中,因此当用户单击后退按钮时,webview会将其返回到previos页面。 但当我按下后退按钮它退出应用程序? 这是webview的代码:
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.emirnp.next.press.R;
@SuppressLint("SetJavaScriptEnabled")
public class VestiActivity extends SherlockFragmentActivity {
private static WebView webview;
private static ProgressBar bar;
private static String url;
public static class WebviewFragment extends SherlockFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
View v = inflater.inflate(R.layout.activity_webview, container,
false);
webview = (WebView) v.findViewById(R.id.webView1);
webview.setWebChromeClient(new MyWebChromeClient());
bar = (ProgressBar) v.findViewById(R.id.load);
url = "http://www.server.se/test/"
+ getResources().getString(R.string.vesti_id);
loadUrl();
return v;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void loadUrl() {
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
final SherlockFragmentActivity activity = getSherlockActivity();
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
if (progress == 100) {
bar.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT)
.show();
}
});
webview.loadUrl(VestiActivity.url);
}
}
@Override
public void onBackPressed() {
if (webview.canGoBack())
webview.goBack();
else
super.onBackPressed();
}
}
WebviewActivity:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.emirnp.next.press.R;
public class WebviewActivity extends SherlockFragmentActivity {
private static WebView webview;
private static ProgressBar bar;
private static String url = null;
protected static void setUrl(String url) {
WebviewActivity.url = url;
}
public static class WebviewFragment extends SherlockFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// getSherlockActivity().requestWindowFeature(Window.FEATURE_PROGRESS);
// getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
View v = inflater.inflate(R.layout.activity_webview, container,
false);
webview = (WebView) v.findViewById(R.id.webView1);
bar = (ProgressBar) v.findViewById(R.id.load);
loadUrl();
return v;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void loadUrl() {
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
final SherlockFragmentActivity activity = getSherlockActivity();
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
/*
* activity.setSupportProgress((Window.PROGRESS_END -
* Window.PROGRESS_START) / 100 * progress);
*/
if (progress == 100) {
bar.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT)
.show();
}
});
webview.loadUrl(url);
}
}
@Override
public void onBackPressed() {
if (webview.canGoBack())
webview.goBack();
else
super.onBackPressed();
}
}
activity_webview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AboutActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
<ProgressBar
android:id="@+id/load"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible" />
</RelativeLayout>
答案 0 :(得分:1)
你设置onBackPressed,但你必须这样做webView:
`webView.setOnKeyListener( new OnKeyListener() { @Override public boolean onKey( View v, int keyCode, KeyEvent event ) { if( keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) { webView.goBack(); return true; }else if( keyCode == KeyEvent.KEYCODE_FORWARD && webView.canGoForward()){ webView.goForward(); return true; } return false; } } );`
亲切的问候!
答案 1 :(得分:0)
只需将一个WebviewClient
对象添加到webview