需求分析

  • 要求将test.html放到assets目录下,使用webview加载test.html文件。如图1所示,web页面中有一个按钮和输入框

图1

  • 点击”登录“后,启动android activity。,如图2所示。图2中的内容为图1输入传过来的内容

图2

实现

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
package com.gallifrey.webviewtest;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView=findViewById(R.id.web_view);

WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true); //设置支持js脚本
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//设置缓存策略
webView.addJavascriptInterface(this,"android");//设置js接口
webView.loadUrl("file:///android_asset/test.html");//访问本地html




}

@JavascriptInterface
public void getUserName(String username){
String temp=username;
Intent data=new Intent(this,SecondActivity.class);
data.putExtra("user",temp);

startActivity(data);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.gallifrey.webviewtest;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

public class SecondActivity extends AppCompatActivity {
private TextView textView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView=findViewById(R.id.tv);
String temp=getIntent().getStringExtra("user");
textView.setText(temp);

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html><head><title>Welcome to Webview</title></head>
<body>

<center>
<input id="username" type="text" style="width:160;height:60;font-size:30;margin-right:10px;border-color:rgb(235, 76, 13);border-width: 10px;"><button style="margin-top:10%; width:100;height:60;font-size:30" onclick="login()">登陆</button>
</center>


<script>
function login() {
var username = document.getElementById("username").value;
window.android.getUserName(username)
}
</script>

</body></html>
  • activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<WebView
android:usesCleartextTraffic="true"
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • activity_second.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".SecondActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:gravity="center"
android:text="测试"/>

</androidx.constraintlayout.widget.ConstraintLayout>

img

img