Android通過WebView調用 JS 代碼

Android通過WebView調用 JS 代碼

1

a.  webView.loadUrl("javascript:callJS()");

b.  setWebChromeClient響應彈窗

1.佈局

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        tools:layout_editor_absoluteX="136dp"

        tools:layout_editor_absoluteY="0dp"

        android:orientation="vertical"

        tools:ignore="MissingConstraints">

        <WebView

            android:id="@+id/webview"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:layout_weight="1">

        </WebView>

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:orientation="horizontal"

            android:layout_weight="3">

            <Button

                android:id="@+id/btnLeft"

                android:layout_weight="1"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:text="左邊" />

            <EditText

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:layout_weight="1"

                android:gravity="center"

                android:text="0"/>

            <Button

                android:id="@+id/btnRight"

                android:layout_weight="1"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:text="右邊" />

        </LinearLayout>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

  

2.index.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<!DOCTYPE html>

<html>

 

<head>

    <meta charset="utf-8">

    <title>Carson_Ho</title>

    // JS代碼

    <script>

    // Android需要調用的方法

    function callJS(){

        document.getElementById("test").innerHTML = "android button 點擊左邊 實現'webView.loadUrl('javascript:callJS()');'就可以響應";

        alert("Android調用了JS的callJS方法 實現‘webView.setWebChromeClient’纔有響應");

    }

</script>

 

</head>

<body>

    <div id="test"> 0000000000 </div>

</body>

</html>

3.java

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

public class MainActivity extends Activity {

    WebView webView;

    Button buttonLeft, buttonRight;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        webView = findViewById(R.id.webview);

        buttonLeft = findViewById(R.id.btnLeft);

        buttonRight = findViewById(R.id.btnRight);

 

        WebSettings webSettings = webView.getSettings();

        //允許使用JS

        webSettings.setJavaScriptEnabled(true);

        // 設置允許JS彈窗

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

 

        webView.loadUrl("file:///android_asset/index.html");

 

        buttonLeft.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                webView.post(new Runnable() {

                    @Override

                    public void run() {

                        webView.loadUrl("javascript:callJS()");

                    }

                });

            }

        });

 

        webView.setWebChromeClient(new WebChromeClient() {

 

            @Override

            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {

                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);

                b.setTitle("alert1");

                b.setMessage(message);

                b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    @Override

                    public void onClick(DialogInterface dialogInterface, int i) {

                        result.confirm();

                    }

                });

                b.setCancelable(false);

                b.create().show();

                return true;

            }

        });

    }

}

 

轉自:https://www.cnblogs.com/liuyj-vv/p/9583831.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章