簡體   English   中英

按鈕:setEnabled(false)不起作用?

[英]Button: setEnabled(false) not working?

我想我只是做了一些愚蠢的事情,但我無法想出這個。

我正在使用GWT,並且我有一個提交按鈕,其中submit通過REST API將一些信息發送到遠程服務器。 問題是,您可以在操作完成時單擊提交多次,並創建多個帖子。

我試過添加

 sendButton.setEnabled(false);

到點擊處理程序,但它似乎沒有工作。 該按鈕保持啟用狀態,我仍然可以根據需要多次點擊該按鈕,從而產生多個帖子。 有人能看出我做錯了嗎? 完整的代碼如下。

public class HelpDeskTest implements EntryPoint {
private final HelpDeskTestServiceAsync helpDeskTest= GWT.create (HelpDeskTestService.class);

final Button sendButton = new Button("Submit");
final TextBox nameField = new TextBox();
final Label errorLabel = new Label();
final TextBox subjectField = new TextBox();
final TextArea descriptionField= new TextArea();


/**
 * This is the entry point method.
 */
public void onModuleLoad() {


    // We can add style names to widgets
    //sendButton.addStyleName("sendButton");

    // Add the nameField and sendButton to the RootPanel
    // Use RootPanel.get() to get the entire body element
    RootPanel.get("nameFieldContainer").add(nameField);
    RootPanel.get("subjectFieldContainer").add(subjectField);
    RootPanel.get("descriptionFieldContainer").add(descriptionField);
    RootPanel.get("sendButtonContainer").add(sendButton);
    RootPanel.get("errorLabelContainer").add(errorLabel);

    //set name field text
    nameField.setText("GWT User");

    // Focus the cursor on the name field when the app loads
    subjectField.setFocus(true);
    subjectField.selectAll();


    //set widths and heights
    descriptionField.setWidth("100%");
    descriptionField.setHeight("200px");
    nameField.setWidth("100%");
    subjectField.setWidth("100%");


    //click handler
    sendButton.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {

            sendButton.setEnabled(false);

            String uName = nameField.getText();
            String subject = subjectField.getText();
            String desc = descriptionField.getText();

            String newURLp1 = "http://xxx.xx.xx/sdpapi/request?" +
                    "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=D4xxxxxxxB6" +
                    "&INPUT_DATA=<?xml version=";
            String urlp2 = "%221.0%22";
            String urlp3 = " encoding=";
            String urlp4 = "%22utf-8%22";
            String urlp5 = "?><Operation><Details><requester>" + uName + "</requester><subject>" + subject + 
            "</subject><description>" + desc + "</description></Details></Operation>";

            String encUrl = URL.encode(newURLp1) + urlp2 + URL.encode(urlp3) + urlp4 + URL.encode(urlp5);
            System.out.println(encUrl);

            helpDeskTest.postToRemoteServer(encUrl,
                    new AsyncCallback<String>() {
                        @Override
                        public void onFailure(Throwable caught)  {
                            Window.alert("Failure getting XML through proxy");
                        }

                        @Override
                        public void onSuccess(String result) {
                            processXML(result);
                        }


                    });
            sendButton.setEnabled(true);
        }
    });

}

    public void processXML(final String xml) {

        try {

            Document doc = XMLParser.parse(xml);

           // get the status using Node's getNodeValue() function - this will determine success or failure.
            String status = doc.getElementsByTagName("status").item(0).getFirstChild().getNodeValue();

            //if success:
            if (status.equals("Success")) {

                String statCode = doc.getElementsByTagName("statuscode").item(0).getFirstChild().getNodeValue();
                String msg = doc.getElementsByTagName("message").item(0).getFirstChild().getNodeValue();
                String woid = doc.getElementsByTagName("workorderid").item(0).getFirstChild().getNodeValue();

                System.out.println("Result from HelpDesk:");
                System.out.println("Status Code: " + statCode);
                System.out.println("Status: " + status);
                System.out.println(msg);

                System.out.println(msg + ".  Ticket Number is: "  + woid);

                errorLabel.setText(msg + ".  Ticket Number is: "  + woid);

            } else if (status.equals("Failed")){
                //get message
                String failmsg = doc.getElementsByTagName("message").item(0).getFirstChild().getNodeValue();
                errorLabel.setText(failmsg);

            } 


        }
        catch ( Exception e ) {
            e.printStackTrace();
        }


    }

}

sendButton.setEnabled(true);

你已經在onClick()方法的末尾再次enabled你的按鈕..看你的點擊處理程序..這可能是問題..

嘗試在onSuccess()方法中移動這一行: -

@Override
public void onSuccess(String result) {
    processXML(result);
    sendButton.setEnabled(true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM