- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
编辑:用户 QuickFix 的回答对我有用。代码就在这个问题的底部。
我正在尝试编写一个 Cordova 3 Android 插件来制作普通和自定义 Toast。但是,我只是一名前端开发人员,对 Cordova 和 Android 还很陌生。我仍在学习,非常感谢您能提供的任何帮助。
到目前为止,我已经成功地单独完成了这两项任务:
我现在的问题是 - 如何让插件在主 Activity 中调用 showCustomToast()
函数?正如您在下面的代码块 #2 中看到的那样,我遇到了如何获取 主要 Activity 以便调用 showCustomToast()
的问题。以下是我目前如何执行此操作的摘录:
// Problem?
HelloCordova main = (HelloCordova) cordova.getActivity();
main.showCustomToast(toastTitle, toastText, duration);
我必须将 cordova.getActivity()
转换为 HelloCordova
,否则它不会识别它具有 showCustomToast ()
函数。但肯定这不是正确的方法,尽管它确实“有效”,即,我能够让自定义 Toast 显示在应用程序中。我只是忍不住觉得我完全走错了路。目前它还不是一个可重复使用的插件!
如果有人能让我走上实现这一目标的正确道路,我将不胜感激。例如,我是否应该完全放弃插件而只放弃 do this?相反?
这是我的第一个 Stackoverflow 问题,如果我需要更改或澄清任何内容,请告诉我。感谢阅读!!
这是我现有的代码:
代码块#1
这个 HelloCordova
类是在启动一个新的 Cordova 项目时自动生成的。我添加了 showCustomToast()
函数。
package io.cordova.hellocordova;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.cordova.*;
public class HelloCordova extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
// Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///android_asset/www/index.html")
}
public void showCustomToast(String toastTitleText, String toastDescText, int toastDuration) {
Toast toast = new Toast(this);
toast.setDuration(toastDuration);
LayoutInflater inflater = getLayoutInflater();
View appearance = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toastRoot));
toast.setView(appearance);
TextView toastTitle = (TextView) appearance.findViewById(R.id.toastTitle);
toastTitle.setText(toastTitleText);
TextView toastDesc = (TextView) appearance.findViewById(R.id.toastDescription);
toastDesc.setText(toastDescText);
toast.show();
}
}
代码块#2
Cordova 插件的 Java 部分。
package com.example.plugins.toast;
//Problem?
import io.cordova.hellocordova.HelloCordova;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class ToastPlugin extends CordovaPlugin {
final String LOG_TAG = "ToastLog";
public static final String ACTION_NORMAL_TOAST = "normalToast";
public static final String ACTION_CUSTOM_TOAST = "customToast";
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
final JSONObject arg_object = args.getJSONObject(0);
final String toastTitle = arg_object.getString("toastTitle");
final String toastText = arg_object.getString("toastText");
final String toastDuration = arg_object.getString("toastDuration");
final CallbackContext ctx = callbackContext;
try {
if (ACTION_NORMAL_TOAST.equals(action)) {
Log.d(LOG_TAG, "Normal toast: " + toastText);
Runnable runnable = new Runnable() {
public void run() {
Context context = cordova.getActivity()
.getApplicationContext();
int duration = Toast.LENGTH_SHORT;
if (toastDuration.equals("LONG")) {
duration = Toast.LENGTH_LONG;
}
Toast.makeText(context, toastText, duration).show();
}
};
this.cordova.getActivity().runOnUiThread(runnable);
callbackContext.success();
return true;
} else if (ACTION_CUSTOM_TOAST.equals(action)) {
Log.d(LOG_TAG, "Custom toast: " + toastTitle + ": " + toastText);
Runnable runnable = new Runnable() {
public void run() {
int duration = Toast.LENGTH_SHORT;
if (toastDuration.equals("LONG")) {
duration = Toast.LENGTH_LONG;
}
//Problem?
HelloCordova main = (HelloCordova) cordova
.getActivity();
main.showCustomToast(toastTitle, toastText, duration);
ctx.success();
}
};
this.cordova.getActivity().runOnUiThread(runnable);
callbackContext.success();
return true;
}
callbackContext.error("Invalid action");
return false;
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
}
}
编辑:这是对我有用的解决方案。正如 QuickFix 在下面的回答中提到的那样,自定义 Toast 代码现在位于插件中。
package com.example.plugins.toast;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class ToastPlugin extends CordovaPlugin {
final String LOG_TAG = "ToastLog";
public static final String ACTION_NORMAL_TOAST = "normalToast";
public static final String ACTION_CUSTOM_TOAST = "customToast";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
final JSONObject arg_object = args.getJSONObject(0);
final String toastTitle = arg_object.getString("toastTitle");
final String toastText = arg_object.getString("toastText");
final String toastDuration = arg_object.getString("toastDuration");
try {
if (ACTION_NORMAL_TOAST.equals(action)) {
Log.i(LOG_TAG, "[Normal toast] toastText: " + toastText);
Runnable runnable = new Runnable() {
public void run() {
Context context = cordova.getActivity().getApplicationContext();
int duration = Toast.LENGTH_SHORT;
if (toastDuration.equals("LONG")) {
duration = Toast.LENGTH_LONG;
}
Toast.makeText(context, toastText, duration).show();
}
};
this.cordova.getActivity().runOnUiThread(runnable);
callbackContext.success();
return true;
} else if (ACTION_CUSTOM_TOAST.equals(action)) {
Log.i(LOG_TAG, "[Custom toast] toastTitle: " + toastTitle + "\n toastText: " + toastText);
Runnable runnable = new Runnable() {
public void run() {
int duration = Toast.LENGTH_SHORT;
if (toastDuration.equals("LONG")) {
duration = Toast.LENGTH_LONG;
}
Context context = cordova.getActivity().getApplicationContext();
Toast toast = new Toast(context);
toast.setDuration(duration);
LayoutInflater inflater = LayoutInflater.from(context);
Resources resources = context.getResources();
String packageName = context.getPackageName();
View appearance = inflater.inflate(resources.getIdentifier("toast_layout","layout",packageName),null);
toast.setView(appearance);
TextView toastTitleView = (TextView) appearance.findViewById(resources.getIdentifier("toastTitle","id",packageName));
toastTitleView.setText(toastTitle);
TextView toastDesc = (TextView) appearance.findViewById(resources.getIdentifier("toastDescription","id",packageName));
toastDesc.setText(toastText);
toast.show();
}
};
this.cordova.getActivity().runOnUiThread(runnable);
callbackContext.success();
return true;
}
callbackContext.error("Invalid action");
return false;
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
}
}
最佳答案
也许您可以将 showCustomToast 放在插件中而不是应用程序中?
在这种情况下,您必须将函数中的 R.layout.layoutname
和 R.id.viewname
替换为
getApplication().getResources().getIdentifier("layoutname","layout",getApplication().getPackageName());
和
getApplication().getResources().getIdentifier("viewname","id",getApplication().getPackageName());
关于java - Cordova 3 Android 插件 - 如何在主要 Activity 中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21012964/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!