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.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDletItemCommmandListener extends MIDlet implements ItemCommandListener,CommandListener,ItemStateListener {
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() {
}
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);
frm.setItemStateListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
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) {
}
public void itemStateChanged(Item i) {
if(i==uid)
{
frm.setTitle("用户名"+uid.getString());
} else if(i==pwd)
{
frm.setTitle("密码"+uid.getString());
}
}
}
张生工作室