Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add javascript interactive #117

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Easily reference the library in your Android projects using this dependency in y

```java
dependencies {
compile 'com.thefinestartist:finestwebview:1.2.7'
compile 'aom.andan:webview:1.2.1'
}
```

Expand Down Expand Up @@ -343,6 +343,39 @@ new FinestWebView.Builder(activity)
.setCustomAnimations(R.anim.activity_open_enter, R.anim.activity_open_exit, R.anim.activity_close_enter, R.anim.activity_close_exit)
.show(url);
```
#javascrpit interactive#
**important** you need to new a class like [JsInteration.java](sample/src/main/java/com/thefinestartist/finestwebview/sample/JsInteration.java)


```java
new FinestWebView.Builder(this).theme(R.style.RedTheme)
.titleDefault("Bless This Stuff")
.webViewBuiltInZoomControls(true)
.webViewDisplayZoomControls(true)
.dividerHeight(0)
.gradientDivider(false)
.webViewJavaScriptEnabled(true)//设置可以和JAVA交互
.addJavascriptInterface(new JsInteration())//JS将可以调用该类下面的@JavascriptInterface修饰的方法,此类须implements Serializable
.setCustomAnimations(R.anim.activity_open_enter, R.anim.activity_open_exit,
R.anim.activity_close_enter, R.anim.activity_close_exit)
.show("http://test.2000new.com/nyd/water/index.html");
```

in yout html code:
```java
<img src="images/booknow.png" onclick="s()" />
<script type="text/javascript">
function s(){

var dd=android.jsToAndroid();
alert(dd);

}
function anroidToJs(param){
alert(param);
}
</script>
```


## More customizations
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.novoda:bintray-release:0.3.4'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.thefinestartist.finestwebview;

import android.app.Activity;
import android.webkit.WebView;

/**
* Created by Administrator on 2017/2/17.
*/

public class BaseJsInteration {

protected Activity instance;
protected WebView webView;

public void setContext(Activity activity){
instance=activity;
}
public Activity getContext(){
return instance;
}

public WebView getWebView() {
return webView;
}

public void setWebView(WebView webView) {
this.webView = webView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import android.support.annotation.StyleRes;
import android.support.design.widget.AppBarLayout.LayoutParams.ScrollFlags;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.thefinestartist.Base;
import com.thefinestartist.finestwebview.enums.Position;
import com.thefinestartist.finestwebview.listeners.BroadCastManager;
Expand Down Expand Up @@ -158,12 +160,13 @@ public static class Builder implements Serializable {
protected Boolean webViewOffscreenPreRaster;

protected String injectJavaScript;
protected BaseJsInteration jsInteration;

protected String mimeType;
protected String encoding;
protected String data;
protected String url;

protected WebView webView;
public Builder(@NonNull Activity activity) {
this.context = activity;
Base.initialize(activity);
Expand All @@ -190,6 +193,11 @@ public Builder addWebViewListener(WebViewListener listener) {
return this;
}

public Builder addJavascriptInterface(BaseJsInteration jsInteration) {
this.jsInteration=jsInteration;
return this;
}

public Builder removeWebViewListener(WebViewListener listener) {
listeners.remove(listener);
return this;
Expand Down Expand Up @@ -892,6 +900,15 @@ public Builder injectJavaScript(String injectJavaScript) {
return this;
}

public WebView getWebView() {
// webView.getSettings().setJavaScriptEnabled(true);
return webView;
}

public void setWebView(WebView webView) {
this.webView = webView;
}

public void load(@StringRes int dataRes) {
load(Res.getString(dataRes));
}
Expand Down
Loading