日期:2014-05-20  浏览次数:20568 次

两个TextField的“清除”按钮怎么用?
Java code
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.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Delete extends MIDlet implements CommandListener{

    private Form form = new Form("“清除”要怎么用?请教各位大虾");
    private Display dis;
    private TextField user = new TextField("用户名:","",20,TextField.ANY );
    private TextField pass = new TextField("密码:","",20,TextField.PASSWORD );
    private Command cmdDel = new Command("清除",Command.SCREEN,1);
    int position;
    
    protected void startApp() throws MIDletStateChangeException {
        dis = Display.getDisplay(this);
        dis.setCurrent(form);
        
        form.append(user);
        form.append(pass);        
        form.addCommand(cmdDel);
        form.setCommandListener(this);
    }
    public void commandAction(Command c, Displayable d) {        
        if(c==cmdDel){
            if("什么"=="用户名"){
                position = user.getCaretPosition();
                user.delete(position-1, 1);
            }
            else if("什么"=="密码"){
                position = pass.getCaretPosition();
                pass.delete(position-1, 1);
            }
        }    
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub
    }
    protected void pauseApp() {
        // TODO Auto-generated method stub
    }

}


就是下面的代码要怎么修改?
Java code
    if(c==cmdDel){
            if("什么"=="用户名"){
                position = user.getCaretPosition();
                user.delete(position-1, 1);
            }
            else if("什么"=="密码"){
                position = pass.getCaretPosition();
                pass.delete(position-1, 1);
            }
        }    


------解决方案--------------------
user.addCommand(cmdDel);
pass.addCommand(cmdDel);
user.setItemCommandListener(this);
pass.setItemCommandListener(this);
用ItemCommandListener吧。
------解决方案--------------------
你给Form中加载的是ITEM 对象 用CommandListener不合适吧
2楼说的很清楚了
另外 
if(position != 0)
{
....
}
就可以了