/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webviewmain); /** android内置浏览器对象 */ webView = (WebView) this.findViewById(R.id.webView); /** * 加载放在assets目录下的android.html文件 注意url的地址前缀为: file:///android_asset/ * * 其实可以把这个html布局文件放在公网中,这样方便随时更新维护 例如 * webview.loadUrl("http://192.168.1.100:8080/Hello/index.html"); */ //webView.loadUrl("file:///android_asset/android.html"); //webView.loadUrl("file:///android_asset/index.html"); /** 允许javascript的运行 */ webView.getSettings().setJavaScriptEnabled(true); final Context myApp = this; /* WebChromeClient must be set BEFORE calling loadUrl! */ webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) { new AlertDialog.Builder(MainActivity.this) .setTitle("javaScript dialog") .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; }; @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this); b.setTitle("Confirm"); b.setMessage(message); b.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); b.setCancelable(false); b.create(); b.show(); return true; }; /* @Override public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) { final LayoutInflater factory = LayoutInflater .from(MainActivity.this); final View v = factory.inflate(R.layout.prompt_dialog, null); ((TextView) v.findViewById(R.id.prompt_message_text)) .setText(message); ((EditText) v.findViewById(R.id.prompt_input_field)) .setText(defaultValue); AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this); b.setTitle("Prompt"); b.setView(v); b.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String value = ((EditText) v .findViewById(R.id.prompt_input_field)) .getText().toString(); result.confirm(value); } }); b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); b.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { result.cancel(); } }); b.show(); return true; };*/ public void onProgressChanged(WebView view, int newProgress) { MainActivity.this.getWindow().setFeatureInt( Window.FEATURE_PROGRESS, newProgress * 100); super.onProgressChanged(view, newProgress); } public void onReceivedTitle(WebView view, String title) { MainActivity.this.setTitle(title); super.onReceivedTitle(view, title); } }); |
|