0

我的代码中有 2 个按钮,“清除”和“登录”,问题是无法单击登录按钮,但清除按钮可以正常工作。我的错误在哪里?

下面的代码是整个项目

public MyScreen()
{        
    // Set the displayed title of the screen       
    Bitmap logo = Bitmap.getBitmapResource("icon.png");
    bitField = new BitmapField(logo, Field.FIELD_HCENTER);
    userField = new EditField("Username :", "");
    passField = new PasswordEditField("Password :", "");
    ddlDomain = new ObjectChoiceField("Domain :", new String[] {"Home", "Work"});
    chkRemember = new CheckboxField("Remember Password", false);
    btnClear = new ButtonField("Clear", ButtonField.CONSUME_CLICK);
    btnLogIn = new ButtonField("Log In", ButtonField.CONSUME_CLICK);
    add(bitField);
    add(new SeparatorField());
    add(new LabelField("Please enter your credentials:"));
    add(userField);
    add(passField);
    add(ddlDomain);
    add(chkRemember);
    HorizontalFieldManager btnManager = new HorizontalFieldManager(Field.FIELD_RIGHT);
    btnManager.add(btnClear);
    btnManager.add(btnLogIn);
    add(btnManager);

    btnClear.setChangeListener(this);
}

public void fieldChanged(Field field, int context) {
    if(field == btnClear){
        clearTextFields();
    }
    else if(field == btnLogIn){
        login();
    }
}
private void clearTextFields(){
    userField.setText("");
    passField.setText("");
}
private void login(){
    if(userField.getTextLength() == 0 || passField.getTextLength() == 0){
        Dialog.alert("You must enter a username and password");
    }
    else{
        String username = userField.getText();
        String selectedDomain = (String)ddlDomain.getChoice(ddlDomain.getSelectedIndex());
        SuccessScreen loginSuccess = new SuccessScreen(username, selectedDomain);
        UiApplication.getUiApplication().pushScreen(loginSuccess);
    }
}
4

1 回答 1

3

嗨亲爱的你需要添加

btnLogIn.setChangeListener(this);

尝试这个

于 2012-08-29T05:45:01.550 回答