我正在尝试通过Javascript函数从网页“查找框”调用。吐司正常工作,但“ search()”函数无效。我认为代码中存在一些问题。请帮我。我也在关注this文章。正常运行,但不能从网页上运行。
activity_search_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layoutId"
android:background="@color/colorPrimary">
</LinearLayout>
</RelativeLayout>
SearchDemoActivity.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Method;
@SuppressWarnings("ALL")
public class SearchDemoActivity extends AppCompatActivity {
WebView mWebView;
private LinearLayout container;
private TextView previusButton, nextButton, closeButton, searchbtn;
private EditText findBox;
@SuppressWarnings( "deprecation" )
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_demo);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.loadUrl("file:///android_asset/main.html");
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient( new WebViewClient() );
mWebView.setWebChromeClient( new WebChromeClient() );
WebSettings settings = mWebView.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
mWebView.addJavascriptInterface(new JavascriptInterface(),"Searchbtn");
mWebView.setWebChromeClient(new WebChromeClient() {
});
}
public class JavascriptInterface {
@android.webkit.JavascriptInterface
public void SearchOnclick(){
search();
Toast.makeText(getApplicationContext(), "You are searching.....", Toast.LENGTH_SHORT).show();
}
}
@SuppressLint("ResourceAsColor")
@SuppressWarnings( "deprecation" )
public void search(){
container = (LinearLayout)findViewById(R.id.layoutId);
closeButton = new TextView(this);
closeButton.setTextSize(25);
closeButton.setText("×");
closeButton.setTextColor(Color.parseColor("#FFFF4059"));
closeButton.setPadding(30, 10,30,10);
closeButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
container.removeAllViews();
findBox.getText().clear();
mWebView.findAll(findBox.getText().toString());
}
});
container.addView(closeButton);
previusButton = new TextView(this);
previusButton.setTextSize(25);
previusButton.setFocusable(true);
previusButton.setClickable(true);
previusButton.setText("<");
previusButton.setPadding(30, 10,30,10);
previusButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mWebView.findNext(false);
}
});
container.addView(previusButton);
nextButton = new TextView(this);
nextButton.setTextSize(25);
nextButton.setText(">");
nextButton.setClickable(true);
nextButton.setFocusable(true);
nextButton.setPadding(30, 10,30,10);
nextButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
mWebView.findNext(true);
}
});
container.addView(nextButton);
searchbtn = new TextView(this);
searchbtn.setTextSize(20);
searchbtn.setText("\uD83D\uDD0D");
searchbtn.setPadding(30, 10,0,10);
searchbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mWebView.findAll(findBox.getText().toString());
}
});
container.addView(searchbtn);
findBox = new EditText(this);
findBox.setMinEms(30);
findBox.setSingleLine(true);
findBox.setPadding(10, 10,30,10);
findBox.setHint("Search...");
findBox.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event){
if((event.getAction() == KeyEvent.ACTION_DOWN) && ((keyCode == KeyEvent.KEYCODE_ENTER))){
mWebView.findAll(findBox.getText().toString());
try{
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(mWebView, true);
}catch(Exception ignored){
}
}
return false;
}
});
container.addView(findBox);
}
}
javascript代码:
var search = document.getElementById('search')
search.addEventListener('click', function(){
Searchbtn.SearchOnclick()
})