java – Android启用webview中的后退按钮
发布时间:2020-12-15 08:43:30 所属栏目:Java 来源:网络整理
导读:我正在使用以下代码在我的 Android应用中显示webview. package com.company.myapp;import com.google.android.apps.analytics.GoogleAnalyticsTracker;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.
我正在使用以下代码在我的
Android应用中显示webview.
package com.company.myapp; import com.google.android.apps.analytics.GoogleAnalyticsTracker; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class ArticlesActivity extends Activity { /** Initialize the Google Analytics Tracker */ GoogleAnalyticsTracker tracker; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_PROGRESS); getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON); WebView webview = new WebView(this); setContentView(webview); setProgressBarVisibility(true); webview.getSettings().setJavaScriptEnabled(true); webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); final Activity activity = this; tracker = GoogleAnalyticsTracker.getInstance(); // Start the tracker,updating google every 20 seconds tracker.start((String) getText(R.string.analyticsID),20,this); webview.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view,int progress) { activity.setProgress(progress * 100 ); } }); webview.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) { Toast.makeText(activity,"Oh no! " + description,Toast.LENGTH_SHORT).show(); } }); webview.loadUrl("http://www.google.com"); } @Override public void onResume() { tracker.trackPageView("ArticlesActivity"); super.onResume(); } @Override protected void onDestroy() { super.onDestroy(); // Stop the tracker when it is no longer needed. tracker.stop(); } } 如果历史存在而不是仅退出webview,我需要启用后退按钮以退回. I’ve tried many different code examples such as this但无法上班.按下后退按钮时,应用程序才会关闭. 这是我的代码与后退按钮代码,但它只是在按下后退按钮时崩溃应用程序: package com.company.myapp; import com.google.android.apps.analytics.GoogleAnalyticsTracker; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class ArticlesActivity extends Activity { WebView webview; /** Initialize the Google Analytics Tracker */ GoogleAnalyticsTracker tracker; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_PROGRESS); getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Toast.LENGTH_SHORT).show(); } }); webview.loadUrl("http://www.google.com"); } @Override public boolean onKeyDown(int keyCode,KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { webview.goBack(); return true; } return super.onKeyDown(keyCode,event); } @Override public void onResume() { tracker.trackPageView("ArticlesActivity"); super.onResume(); } @Override protected void onDestroy() { super.onDestroy(); // Stop the tracker when it is no longer needed. tracker.stop(); } } 有人可以帮我解决一下吗? 解决方法
得到它了!你在这一行的问题
WebView webview = new WebView(this); 您正在函数内部创建变量,而不是使用您的成员变量,因此您的成员变量在onKeyDown函数内为null. 只需更换它 webview = new WebView(this); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读