결과화면 

// WebViewDemoA3.java

 

package com.example.graphic;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

public class WebViewDemoA3 extends Activity {
 private WebView webview;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.webview_demo);
  
  webview = (WebView)this.findViewById(R.id.webview);
  webview.getSettings().setJavaScriptEnabled(true);
  
  MyWebChromeClient mywebChromeClient = new MyWebChromeClient();  
  webview.setWebChromeClient(mywebChromeClient);
  webview.loadUrl("file:///android_asset/jsalert.html");
 }
 
 private class MyWebChromeClient extends WebChromeClient{

  @Override
  public boolean onJsAlert(WebView view, String url, String message,
    JsResult result) {
   Toast.makeText(WebViewDemoA3.this, message, Toast.LENGTH_SHORT).show();   
   result.confirm();   
   return true;
  }
 }
}

 

 

 

 

 

// webview_demo.xml

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

 

 

 

<!-- 매니페스트에 권한 추가!!! -->

<uses-permission android:name="android.permission.INTERNET" />

 

--------------------------------------------------------------------------------------------------

 

// assets 폴더에 jsalert.html 넣어주자! 코드는 밑과 다음과 같다....

 

 

<html>
<head>
<title>경고창</title>

<body>
<h2>
<a href="javascript:alert('경고창입니다.');"> 경고창 보기</a>
</h2>
</body>
</html>

 

+ Recent posts