如何创建土司的PhoneGap? [英] How to create toast in phonegap?
本文介绍了如何创建土司的PhoneGap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用PhoneGap的/科尔多瓦创建土司在Android应用程序?
How to create toast in android application using phonegap / cordova?
感谢名单!
推荐答案
首先创建一个 ToastPlugin.java
package com.company.plugins;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
import android.widget.Toast;
public class ToastPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
String message = args.getString(0);
// used to log the text and can be seen in LogCat
Log.d("Toast Plugin", "Calling the Toast...");
Log.d("Toast Plugin", message);
if (action.equals("shortToast")) {
this.shortToast(message, callbackContext);
return true;
} else if (action.equals("longToast")) {
this.longToast(message, callbackContext);
return true;
}
return false;
}
private void shortToast(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
Toast.makeText(cordova.getActivity().getApplicationContext(),
message, Toast.LENGTH_SHORT).show();
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
private void longToast(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
Toast.makeText(cordova.getActivity().getApplicationContext(),
message, Toast.LENGTH_LONG).show();
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
}
然后创建一个 toastPlugin.js
Then create a toastPlugin.js
//Plugin file should be always after cordova.js
//There is always better way to create, but this also works
window.shortToast = function(str, callback) {
cordova.exec(callback, function(err) {
callback('Nothing to echo.');
}, "ToastPlugin", "shortToast", [ str ]);
};
window.longToast = function(str, callback) {
cordova.exec(callback, function(err) {
callback('Nothing to echo.');
}, "ToastPlugin", "longToast", [ str ]);
};
在项目中链接这些文件,现在你可以在的JavaScript
的呼叫:
Link these files in your project, now you can call in JavaScript
as :
- shortToast(短吐司消息在这里......);
- longToast(龙吐司消息在这里......);
这篇关于如何创建土司的PhoneGap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文