android 应用程序上的后退按钮在 android 2.3 中不起作用,并且在 eclipse 和 ubuntu 系统上开发应用程序,我的模拟器也没有启用后退按钮,我什至不知道什么不起作用当创建 apk 并安装我的手机时它不起作用?请帮我解决这个问题。
MainActivity.java:
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView myWebView;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
//myWebView.loadUrl("http://www.example.com");
//WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
//setting JS to work in html pages in app
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//myWebView.setInitialScale(50);
myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
myWebView.loadUrl("file:///android_asset/www/testhtml5.html");
//mWebView.loadUrl("http://beta.html5test.com/");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
最佳答案
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
编辑:
添加此声明:
WebView myWebView;
在 onCreate() 方法之前的 Activity 内部,您应该可以开始了。
关于java - 安卓和java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21164178/