2013年12月2日 星期一

App Javascript 對話方塊

Webhomefile1.java
package com.example.theoryproject1;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

public class Webhomefile1 extends Activity implements android.view.View.OnClickListener
{
private WebView wv1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.webhomwfile1);

wv1=(WebView)findViewById(R.id.webView1);

WebSettings wset=wv1.getSettings();
wset.setJavaScriptEnabled(true);
wset.setBuiltInZoomControls(true);

WebChromeClient wcc=new WebChromeClient()
{
public boolean shouldOverrideUrlLoading(WebView view,String url)
{
view.loadUrl("file:///android_asset/hopealert11.html");
return true;//非繼承需自行輸入
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub

return super.onJsAlert(view, url, message, result);
}

@Override
public boolean onJsConfirm(WebView view, String url,
String message, JsResult result) {
// TODO Auto-generated method stub
return super.onJsConfirm(view, url, message, result);
}

@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, JsPromptResult result) {
// TODO Auto-generated method stub
return super.onJsPrompt(view, url, message, defaultValue, result);
}

};

wv1.setWebChromeClient(wcc);

Button bt1=(Button)findViewById(R.id.button1);
   bt1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wv1.loadUrl("file:///android_asset/hopealert11.html");

}
}
-------------------
webhomefile.jml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="載入網頁" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

    </LinearLayout>

</LinearLayout>
hopealert11.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
</head>
<script language="javascript">
function  fun1()
{
alert("訊息方塊");
}
function fun2()
{
var k=confirm("到goole網站嗎?");
if (k=true)
{
location.href="http://www.google.com.tw";
}
}
function fun3()
{
var t=prompt("輸入國文分數");
alert("國文:" +t);
}
</script>

<body>
<a href="Javascript:void();" onclick="fun1()">啟動輸出對話方塊</a>
<br />
<a href="Javascript:void();" onclick="fun3()">啟動輸入對話方塊</a>
<br />
<a href="Javascript:void();" onclick="fun2()">啟動確認對話方塊</a>
<br />


</body>
</html>
----------------------------

沒有留言:

張貼留言