Android通过WebView调用 JS 代码

7 篇文章 0 订阅
4 篇文章 0 订阅

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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值