我使用webview提交html表单和相同的表单按钮来打印表单。
但是当我使用界面时会出现某种问题。
Toast消息显示但不创建打印作业create WebPrintJob(myWebView);
package mascoli.printweb;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.print.PrintJob;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Build;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.content.Context;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class WebPrintActivity extends AppCompatActivity {
WebView myWebView;
@Override
protected void onStart() {
super.onStart();
myWebView=(WebView)findViewById(R.id.myWebView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().getBuiltInZoomControls();
myWebView.setInitialScale(139);
myWebView.getSettings().setSaveFormData(false);
myWebView.getSettings().setJavaScriptEnabled(true);
WebSettings ws = myWebView.getSettings();
ws.setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app");
myWebView.loadUrl("http://192.168.1.67/ford/web.html");
}
/*
* JavaScript Interface. Web code can access methods in here
* (as long as they have the @JavascriptInterface annotation)
*/
public class WebViewJavaScriptInterface{
private Context context;
/*
* Need a reference to the context in order to sent a post message
*/
public WebViewJavaScriptInterface(Context context){
this.context = context;
}
/*
* This method can be called from Android. @JavascriptInterface
* required after SDK version 17.
*/
@JavascriptInterface
public void makeToast(String message, boolean lengthLong){
Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show();
createWebPrintJob(myWebView); //PROBLEM HERE
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_print:
createWebPrintJob(myWebView);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = " Print form";
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_print);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_web_print, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
if (myWebView.canGoBack()){
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
return super.onKeyDown(keyCode, event);
}
}
<!DOCTYPE html>
<html>
<head>
<title>JavaScript View</title>
<script type="text/javascript">
function showToast(){
var message = document.getElementById("message").value;
var lengthLong = document.getElementById("length").checked;
/*
Call the 'makeToast' method in the Java code.
'app' is specified in MainActivity.java when
adding the JavaScript interface.
*/
app.
app.makeToast(message, lengthLong);
return false;
}
/*
Call the 'showToast' method when the form gets
submitted (by pressing button or return key on keyboard).
*/
window.onload = function(){
var form = document.getElementById("form");
form.onsubmit = showToast;
}
</script>
</head>
<body>
<form id="form">
Message: <input id="message" name="message" type="text"/><br />
Long: <input id="length" name="length" type="checkbox" /><br />
<input type="submit" value="Make Toast" />
</form>
</body>
</html>