忽視。我打開了安裝的錯誤應用程序。它奇妙地工作。 :)帶有後退按鈕的Android Web視圖,如果沒有其他
我有我的webview中正確工作的後退按鈕,但我想知道的東西。 如何使webview回退到不再存在,而不是退出程序,而是打開一個對話框,詢問用戶是否確定不會退出。 這是我的代碼。
感謝您抽出時間。
java文件
package com.vtd.whatthe;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WhatThe extends Activity {
private WebView webview;
/** Called when the activity is first created. */
public void onBackPressed(){
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
}
else {
openMyDialog(null);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(50);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://test2.com/");
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public void openMyDialog(View view) {
showDialog(10);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 10:
// Create our AlertDialog
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit? You have unlimited guesses!")
.setCancelable(true)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// Ends the activity
WhatThe.this.finish();
}
})
.setNegativeButton("Keep Guessing!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(),
"Good Luck!",
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return super.onCreateDialog(id);
}
}
高興。無論如何,你可能要閱讀[這篇博文](http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html)。 – 2011-06-02 21:40:59