import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDletItemCommmandListener extends MIDlet implements ItemCommandListener,CommandListener {
private Form frm=new Form("Item监听器");
private TextField uid=new TextField("用户名","",20,TextField.ANY);
private TextField pwd=new TextField("密码","",20,TextField.PASSWORD);
private Display dis=Display.getDisplay(this);
private Command Del=new Command("删除",Command.ITEM,1);
public MIDletItemCommmandListener() {
// TODO Auto-generated constructor stub
}
protected void startApp() throws MIDletStateChangeException {
frm.append(uid);
uid.addCommand(Del);
pwd.addCommand(Del);
pwd.setItemCommandListener(this);
uid.setItemCommandListener(this);
frm.append(pwd);
dis.setCurrent(frm);
dis.setCurrentItem(pwd);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
}
public void commandAction(Command c, Item i) {
if(c==Del)
{
TextField tf=(TextField)i;
int p=tf.getCaretPosition();
if(p>0)
{
tf.delete(p-1, 1);
}
}
}
public void commandAction(Command c, Displayable d) {
}
}
张生工作室