我有一堂课,我正在写三个不同的警报对话。这三个警告对话框极大地扩展了该类的代码行。所以为了重构的目的,我想为三个警报对话创建基类,并在主要活动中使用这个类。谁能建议我如何实现这一目标?我的三个警报对话如下:
public boolean onJsConfirm(WebView view, String url, String message,
final android.webkit.JsResult result) {
new AlertDialog.Builder(currentActivity)
.setTitle("Confirmation")
.setMessage(message)
.setPositiveButton(android.R.string.yes,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
})
.setNegativeButton(android.R.string.no,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.cancel();
}
}).setCancelable(false).create().show();
return true;
}
public boolean onJsAlert(WebView view, String url, String message,
final android.webkit.JsResult result) {
new AlertDialog.Builder(currentActivity)
.setTitle("Alert !")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
}).setCancelable(false).create().show();
return true;
}
public void onGeolocationPermissionsShowPrompt(final String origin,
final GeolocationPermissions.Callback callback) {
final boolean remember = true;
AlertDialog.Builder builder = new AlertDialog.Builder(
WebviewActivity.this);
builder.setTitle("Locations");
builder.setMessage(" Would like to use your Current Location")
.setCancelable(true)
.setPositiveButton("Allow",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
callback.invoke(origin, true, remember);
SharedPreferences pref = currentActivity
.getPreferences(currentActivity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref
.edit();
editor.putBoolean("isLocationAvailable",
true);
editor.commit();
webview.loadUrl(getUrl(gps.getLatitude(),
gps.getLongitude(), "true"));
}
})
.setNegativeButton("Don't Allow",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
callback.invoke(origin, false, remember);
webview.loadUrl(getUrl("", "", "false"));
}
});
AlertDialog alert = builder.create();
alert.show();