代号一 - Toast 插件

Codename One - Toast plugin

我正在使用 GUI 构建器构建一个应用程序,并希望显示 toast 消息。在插件描述中显示了这种用途:

Toast.makeText(context, "Hi there", Toast.LENGTH_LONG).show();

"where context is the object passed in the init method of your main class",但是在使用 GUI 生成器时,没有 init 方法。

有什么解决办法吗?

Main中有init方法class(eg:TestToast.java)并创建一个public和对象类型的静态变量context并在init方法和whole中设置它的值代码如下。

public class TestToast {

private Form current;
   **public static Object context;**
public void init(Object context) {
public class TestToast {

private Form current;
public static Object context;
public void init(Object context) {

    **this.context = context;**
}

public void start() {
    if(current != null){
        current.show();
        return;
    }
    new StateMachine("/theme");        
}

public void stop() {
    current = Display.getInstance().getCurrent();
}

public void destroy() {
}
}

你可以像下面这样显示toast

try {
       **Toast.makeText(TestToast.context, msg, Toast.LENGTH_LONG).show();**

    } catch (Exception e) {
        showDialog(msg);
    }