mirror of
https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
synced 2026-04-28 00:00:04 +08:00
#4 Upgrading the JS API to prepare for custom options (no additional options yet, this is a change to make that possible in the future)
This commit is contained in:
@@ -5,6 +5,7 @@ import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/*
|
||||
// TODO nice way for the Toast plugin to offer a longer delay than the default short and long options
|
||||
@@ -33,13 +34,18 @@ public class Toast extends CordovaPlugin {
|
||||
return true;
|
||||
}
|
||||
|
||||
final String message = args.getString(0);
|
||||
final String duration = args.getString(1);
|
||||
final String position = args.getString(2);
|
||||
final JSONObject options = args.getJSONObject(0);
|
||||
|
||||
final String message = options.getString("message");
|
||||
final String duration = options.getString("duration");
|
||||
final String position = options.getString("position");
|
||||
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
android.widget.Toast toast = android.widget.Toast.makeText(webView.getContext(), message, 0);
|
||||
android.widget.Toast toast = android.widget.Toast.makeText(
|
||||
webView.getContext(),
|
||||
message,
|
||||
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
|
||||
|
||||
if ("top".equals(position)) {
|
||||
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20);
|
||||
@@ -52,15 +58,6 @@ public class Toast extends CordovaPlugin {
|
||||
return;
|
||||
}
|
||||
|
||||
if ("short".equals(duration)) {
|
||||
toast.setDuration(android.widget.Toast.LENGTH_SHORT);
|
||||
} else if ("long".equals(duration)) {
|
||||
toast.setDuration(android.widget.Toast.LENGTH_LONG);
|
||||
} else {
|
||||
callbackContext.error("invalid duration. valid options are 'short' and 'long'");
|
||||
return;
|
||||
}
|
||||
|
||||
toast.show();
|
||||
mostRecentToast = toast;
|
||||
callbackContext.success();
|
||||
|
||||
Reference in New Issue
Block a user