亲爱的朋友们请帮助我......并且还使用复选框(CheckboxField)点击一些代码来记住用户名(EditField)和密码(PasswordEditField)......
import java.io.IOException;
import java.util.Hashtable;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.container.MainScreen;
public class LoginFormMainScreen extends MainScreen {
Hashtable persistentHashtable;
CheckboxField checkboxField;
PersistentObject persistentObject;
static final long KEY = 0x9df9f961bc6d6baL;
PasswordEditField passwordField;
EditField editField;
ButtonField loginbutton;
public LoginFormMainScreen() throws IOException {
setTitle("Persistence Test");
editField = new EditField("username:", "");
add(editField);
passwordField=new PasswordEditField("password:","");
add(passwordField);
checkboxField = new CheckboxField("Rememberme", false);
add(checkboxField);
add(new ButtonField("Login"));
persistentObject = PersistentStore.getPersistentObject(KEY);
if (persistentObject.getContents() == null) {
persistentHashtable = new Hashtable();
persistentObject.setContents(persistentHashtable);
}
else {
persistentHashtable = (Hashtable)persistentObject.getContents();
}
if (persistentHashtable.containsKey("EditData")) {
editField.setText((String)persistentHashtable.get("EditData"));
}
if (persistentHashtable.containsKey("BoolData")) {
Boolean booleanObject = (Boolean)persistentHashtable.get("BoolData");
checkboxField.setChecked(booleanObject.booleanValue());
if(booleanObject.booleanValue()==true){
saveChecked();
}
}
}
public void saveChecked() throws IOException {
persistentHashtable.put("", editField.getText());
persistentHashtable.put("", passwordField.getText());
persistentHashtable.put("BoolData", new Boolean(checkboxField.getChecked()));
persistentObject.commit();
}
}
列出项目
答案 0 :(得分:1)
可能是您使用空白键“”将用户名和密码放入哈希表中,但尝试使用“EditData”键检索它。
static class PersistentHashtable extends Hashtable implements Persistable {
}
PersistentHashtable persistentHashtable;
public LoginFormMainScreen() throws IOException {
...
if (persistentHashtable.containsKey("userName")) {
editField.setText((String)persistentHashtable.get("userName"));
}
if (persistentHashtable.containsKey("password")) {
password.setText((String)persistentHashtable.get("password"));
}
...
}
public void saveChecked {
persitentHashtable.put("userName", editField.getText());
persitentHashtable.put("password", password.getText());
persistentHashtable.put("BoolData", new Boolean(checkboxField.getChecked()));
persistentObject.commit();
}