Say you have three inputs; 2 text boxes and 1 button.
In HTML, you would have something like:
<p> Tell me IP address: </p> <input id="myIpInput" type="text">
<p> Tell me IP port: </p> <input id="myPortInput" type="number">
<p> Click to send test connection: </p> <input id="myTestConnectionButton" type="button">
Now these three inputs are referenced in Java by their “id” attribute.
So in Java we can use them as:
// Register the IP address input
@Input(id="myIpInput")
InputTextField MyIPaddress;
// Register the port input
@Input(id="myPortInput")
InputTextField MyPort;
// Register the button to test connection input
@Input(id="myTestConnectionButton")
InputButton TestConnectionButton;
Now, we have registered objects for refrencing the inputs.
Hence, we can now interact in terms of getting and setting values.
E.g. String thisIP = MyIPaddress.getText();
or for setting the button text TestConnectionButton.setText("Click Me");
In order to register a callback function that executes, when the user interacts with out inputs, we need to provide a public method, that is annotated with the correct “Input” attribute and accepts the correct “event” as an argument.
// Callback when user enters IP address
@Input(id="myIpInput")
public void someNameThatSuggestsIpAddressChange(InputEvent event){
// Here, we check that the event was actually a change, otherwise do nothing
if(event.getEventType() == InputEvent.EventType.ON_CHANGE){
String thisIP = MyIPaddress.getText();
// Do something with the newly read input value, e.g. storing to DataModel
}
}
Hence, the linking of the @Input
-annotation, the correct id
for this annotation, a public
method and that it accepts the event type that occured (e.g. InputEvent
, SelectEvent
or TouchEvent
) as argument will - combined - result in your callback being called correctly. The actual name of your function, i.e. “onMessageChange” or “someNameThatSuggestsIpAddressChange” have no impact.
So for the remaining two inputs, we could do:
// Callback when user enters IP address
@Input(id="myPortInput")
public void someChangedInPortSetting(InputEvent event){
// Here, we check that the event was actually a change, otherwise do nothing
if(event.getEventType() == InputEvent.EventType.ON_CHANGE){
String port = MyPort.getText();
// Do something with the newly read input value, e.g. storing to DataModel
}
}
// Callback when user clicks the button
@Input(id="myTestConnectionButton")
public void executeConnectionTestAction(InputEvent event){
// Here, we decided that it should happen when the button is PRESSED, hence ON_PRESSED
// We could also select to fire the event, when the user released the button - ON_RELEASED
if(event.getEventType() == InputEvent.EventType.ON_PRESSED){
// Call the functional code for that connection test here...
foo(bar);
}
}