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

如何判断TextField为空
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMIDlet extends MIDlet implements CommandListener
{
private Display display;
private Command quit;
private Command ok;
private Command back;
private Form f;
private TextField name;
private TextField password;
public HelloMIDlet()
{
display=Display.getDisplay(this);
ok=new Command("确认",Command.OK,1);
quit=new Command("取消",Command.EXIT,1);
back=new Command("返回",Command.SCREEN,1);
name=new TextField("姓名:","",8,TextField.ANY);
password=new TextField("密码:","",12,TextField.PASSWORD);
f=new Form("登陆");
f.append(name);
f.append(password);
f.addCommand(quit);
f.addCommand(ok);
f.setCommandListener(this);
display.setCurrent(f);
}

protected void destroyApp(boolean arg0){

}

protected void pauseApp() {

}

protected void startApp(){


}

public void commandAction(Command arg0, Displayable arg1) {
if(arg0==quit)
{
destroyApp(true);
notifyDestroyed();
}
if(arg0==ok)
{

if(name.getString()==""||password.getString()=="")//为什么这里在未输入如何信息时它还是不执行
{
Form f1=new Form("错误");
f1.append("姓名和密码不能为空");
f1.addCommand(back);
f1.setCommandListener(this);
display.setCurrent(f1);

}else
{
Form f1=new Form("信息");
String str=name.getString();
String str1=password.getString();
str="你的姓名是:"+str;
str1="你的密码是:"+str1;
f1.addCommand(back);
f1.append(str);
f1.append(str1);
f1.setCommandListener(this);
display.setCurrent(f1);
}

}
if(arg0==back)
{
display.setCurrent(f);
}

}

}

------解决方案--------------------
if(str==null || str.trim().length==0){
// it is blank of null
}