CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus 255
Now we’re ready to get into the “meat” of our event processing (handling) code inside of the
onClick( ) method, which will take the data value from the EditText UI object, parse the data, and
write it into the Galaxy object variables (characteristics).
Transferring EditText Data to the Galaxy Object Using .parse( )
Methods
Inside of the onClick( ) method, set the Galaxy object named milkyWay galaxyColonies variable
equal to the result of the .parseLong( ) method, which is passed the result of the data value which
is obtained using method chaining to call a .getText( ) method off of the colonyEdit EditText object
and then convert that data to String data using the .toString( ) method call. This is all accomplished
using the following very dense line of Java code:
MainActivity.milkyWay.galaxyColonies = Long.parseLong(colonyEdit.getText( ).toString( ));
Working backward from the UI object, you get the text value from the colonyEdit EditText object
using the .getText( ) method, make sure that the value is String data using the .toString( ) method,
pass that String data to the .parseLong( ) method which will extract any number values from
the String and make sure they are Long data values, and then set the Long data values to the
galaxyColonies variable of the milkyWay Galaxy object in the MainActivity class.
The entire event handling Java code structure for the colonyButton object should look like the
following code:
colonyButton.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v) {
MainActivity.milkyWay.galaxyColonies = Long.parseLong( colonyEdit.getText( ).toString( ) );
}
});
Now you can copy this event handling code structure three more times underneath itself, and
change the colony UI object (Button and EditText) references to pop, fleets, and ships, respectively,
as is shown in Figure 7-39.