我正在为我的应用程序编写帮助系统。我在 AlertDialog 中使用 WebView。HTML 帮助页面存储在 assets/ 目录中,并具有相互链接引用。重现问题的最少代码:
package qa.so.wv;
import android.content.Context;
import android.app.AlertDialog;
import android.webkit.WebView;
import android.content.DialogInterface;
import android.view.ContextThemeWrapper;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class DialogInformation {
public void showFile(Context context, String file) {
AlertDialog.Builder dialog = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.InformationDialogStyle));
final Context ctx = context;
final WebView wv = new WebView(context);
wv.setWebViewClient(
new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
);
WebSettings settings = wv.getSettings();
settings.setDefaultTextEncodingName("utf-8");
wv.loadUrl(
"file:///android_asset/"+file
);
dialog.setView(wv);
dialog.setPositiveButton(
context.getString(R.string.cont),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
dialog.setNeutralButton(
"Back",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (wv.isFocused() && wv.canGoBack()) {
wv.goBack();
} else {
Toast.makeText(
ctx,
"No go back for Back button",
Toast.LENGTH_LONG
).show();
}
}
}
);
dialog.setOnCancelListener(
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (wv.isFocused() && wv.canGoBack()) {
wv.goBack();
} else {
Toast.makeText(
ctx,
"No go back for system return 'hardware' button",
Toast.LENGTH_LONG
).show();
}
}
}
);
dialog.show();
}
}
此类在此处使用:
package qa.so.wv;
import android.os.Bundle;
import android.app.ListActivity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.view.MenuItem;
import android.view.Menu;
import android.widget.Toast;
import android.webkit.WebView;
public class ShowArchive extends ListActivity
{
// options menu items constants
protected static final int MENU_HELP = Menu.FIRST+11;
protected static final int MENU_QUIT = Menu.FIRST+16;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/* Create Menu Items */
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_HELP, 0, R.string.menu_help);
menu.add(0, MENU_QUIT, 0, R.string.menu_quit);
return true;
}
/* Handles Menu Item Selection */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_HELP:
DialogInformation dn = new DialogInformation();
dn.showFile(
this,
getString(R.string.help_file)
);
return true;
case MENU_QUIT:
finish();
return true;
}
return false;
}
}
以及 assets/ 目录页面一中的两个示例页面:
<html>
<a href="second.html">link to 2-nd file"</a>
<p>
Some text.
</p>
</html>
第二页:
<html>
<p>2-nd file</p>
</html>
我从菜单中调用“帮助”。显示第一页。我点击链接。第二页正确加载。 当我单击“返回”按钮或硬件“取消”按钮时,帮助对话框消失,而不是加载先前显示的页面。
请帮我解决这个问题。