主要活动的问题,所以我可以调用 showCustomToast()。下面是我目前如何做的提取:// Problem?HelloCordova main = (HelloCordova) cordova.getActivity();main.showCustomToast(toastTitle, toastText, duration);I have to cast cordova.getActivity() to HelloCordova, otherwise it won't recognise that it has the showCustomToast() function. But surely this is not the correct approach, although it does "work", ie, I am able to get custom Toast to show in the app. I just can't help but feel that I've gone about this completely the wrong way. It's not exactly a reusable plugin at the moment!我必须演示 cordova.getActivity()到 HelloCordova ,否则它不会识别它 showCustomToast功能。但这肯定是不是正确的方法,虽然它工作,即,我能够得到自定义Toast在应用程序中显示。我只是不能不感到我已经走过这完全是错误的方式。现在它不是一个可重复使用的插件。I would be very grateful if someone could set me on the right path of how to achieve this. For example, should I give up on the plugin completely and just do this instead?如果有人能让我如何实现这一点,我将非常感谢。例如,我应该完全放弃插件,然后执行此操作 ?This is my first Stackoverflow question so please let me know if I should change or clarify anything. Thank you for reading!!这是我的第一个Stackoverflow问题,所以请让我知道,如果我应该更改或澄清任何东西。谢谢您阅读!Here's my existing code:这是我现有的代码: 代码块# b>This HelloCordova class was automatically generated when starting a new Cordova project. I added the showCustomToast() function.这个 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(); }}Code block #2 代码块#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; } }}Edit: Here is the solution that worked for me. As QuickFix mentioned in their answer below, the custom toast code is now in the plugin. 是为我工作的解决方案。正如下面的答案中提到的QuickFix一样,自定义Toast代码现在在插件中。推荐答案在这种情况下你必须在函数中替换 R.layout.layoutname 和 R.id.viewname 与In that case you will have to replace in the function the R.layout.layoutname and R.id.viewname withgetApplication().getResources().getIdentifier("layoutname","layout",getApplication().getPackageName());和getApplication().getResources().getIdentifier("viewname","id",getApplication().getPackageName()); 这篇关于Cordova 3 Android插件 - 如何在主活动中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-04 22:19