import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText shuru;
private WebView webs;
private static final String HTML_URL = “http://172.17.8.100/images/small/default/test.html”;
@SuppressLint(“JavascriptInterface”)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initwebpeizhi();
webs.addJavascriptInterface(new Object(){
//必须加这个注解
@JavascriptInterface
public void showToast(String toast){
Toast.makeText(getApplicationContext(),""+toast,Toast.LENGTH_SHORT).show();
}
},“Android”);
}
private void initwebpeizhi() {
webs.setWebViewClient(new WebViewClient());
WebSettings settings = webs.getSettings();
settings.setJavaScriptEnabled(true);
}
public void chaxun(View view) {
webs.loadUrl(HTML_URL);
}
private void initView() {
shuru = (EditText) findViewById(R.id.shuru);
webs = (WebView) findViewById(R.id.webs);
}
private void submit() {
// validate
String shuruString = shuru.getText().toString().trim();
if (TextUtils.isEmpty(shuruString)) {
Toast.makeText(this, “shuruString不能为空”, Toast.LENGTH_SHORT).show();
return;
}
}
public void jsdy(View view) {
webs.loadUrl(“javascript:changeInputValue(‘理元旗JS调用成功’)”);
}
}
<?xml version="1.0" encoding="utf-8"?>