我正在创建一个应用程序,当应用程序启动时,用户可以选择将文件添加到驻留在存储卡上的应用程序。有一个名为add new
的按钮;当用户点击add new
时,会出现一个表单,用户将在其中输入文件名并添加文件,但添加新文件的命令不起作用,是否有人建议我出错?
我的代码段是
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.file.*;
import javax.microedition.io.Connector;
import java.io.IOException;
import java.util.*;
public class HelloMIDlet
extends MIDlet
implements CommandListener {
private List list;
private Alert alert;
private Display display;
private Form form;
private TextField fname,fpath;
private Command select,remove,add,fadd,back,exit;
public HelloMIDlet() {
form=new Form("add new:");
fname=new TextField("enter File Name","",40,TextField.ANY);
fpath=new TextField("enter File path","file:///SDCard/",50,TextField.ANY);
list = new List("Welcome", List.IMPLICIT);
remove=new Command("Remove Selected",Command.SCREEN,2);
exit=new Command("Exit",Command.EXIT,0);
select=new Command("Select",Command.SCREEN,1);
add=new Command("Add New",Command.SCREEN,2);
list.addCommand(exit);
list.addCommand(select);
list.addCommand(add);
list.addCommand(remove);
form.append(fname);
form.append(fpath);
list.setCommandListener(this);
}
public void startApp() {
display=Display.getDisplay(this);
display.setCurrent(list);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) {
if(c==exit)
{
notifyDestroyed();
}
else if(c==add)
{
// display.setCurrent(form);
addfile();
}
else if(c==remove)
{
}
else if(c==back)
{
display.setCurrent(list);
}
else if(c==fadd)
{
alert=new Alert("Open the file:","Would you like to open the current file??",null,null);
alert.setTimeout(Alert.FOREVER);
}
else if(c==list.SELECT_COMMAND)
{
}
}
public void addfile()
{fadd=new Command("add File",Command.SCREEN,0);
back=new Command("Back",Command.BACK,1);
form.addCommand(fadd);
form.addCommand(back);
form.setCommandListener(this);
display.setCurrent(form);
}
}
答案 0 :(得分:1)
当您选择"添加文件"时,该命令由commandAction
触发和处理,具体来说,这段代码:
else if(c==fadd)
{
alert=new Alert("Open the file:","Would you like to open the current file??",null,null);
alert.setTimeout(Alert.FOREVER);
// You were creating a Alert instance, but not showing it, this line below is one you
// were missing
display.setCurrent(alert, list);
}
您可以使用setCurrent(Alert alert, Displayable nextDisplayable)
或setCurrent(Displayable nextDisplayable)
,具体取决于您的需求。