what is this kind of window that pops up referred to as?
and is there a way to apply it to my webvie开发者_Python百科w for popups? (I can ask this in a separate question if needed)
I suppose you want to show a popup windows which contain a webview from a webview in your application.
Two steps needed. First you need to Override the onJSAlert() method in the WebChromeClient class to enable popup window in webview:
public class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) {
final JsResult finalJsResult = jsResult;
new AlertDialog.Builder(view.getContext()).setMessage(message).setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finalJsResult.confirm();
}
}).setCancelable(false).create().show();
return true;
}
}
and add this to your webview:
MyWebChromeClient myWebChromeClient = new MyWebChromeClient();
webView.setWebChromeClient(myWebChromeClient);
Then you can add custom webview in your AlertDialog (to replace the AlertDialog above):
public OnClickListener imageButtonViewOnClickListener = new OnClickListener() {
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater.from(MyActivity.this);
View alertDialogView = inflater.inflate(R.layout.alert_dialog_layout, null);
WebView myWebView = (WebView) findViewById(R.id.DialogWebView);
myWebView.loadData(webContent, "text/html", "utf-8");
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setView(alertDialogView);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
};
The xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<webView android:id="@+id/DialogWebView" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_marginLeft="20dip"
android:layout_marginRight="20dip" android:textAppearance="?android:attr/textAppearanceMedium" />
精彩评论