The personal wiki of ...

Exercise: Input and Output

Printing on the terminal and getting input via the terminal is not the best way to interact with the phone. This exercise introduces some new ways using features specific to the mobile phone.

Displaying Output

This script show two ways to display output.

import android
droid = android.Android()

# toast messages appear on the screen and disappear quickly                     
droid.makeToast('Hey there')

# a dialog box displays a message and wait until the user does something              
# like press OK                                                                 
droid.dialogCreateAlert('This is a title', 'This is an alert')
droid.dialogSetPositiveButtonText('OK') 
droid.dialogShow()

Getting Input and Displaying Output

What about getting input and displaying the input? You can use raw_input for input via the terminal but this is ugly. Let's try a different way.

import android
droid = android.Android()

# display a dialog to get input with the title Hello! and ask What is your name
# get the result and store it in the variable name
name = droid.getInput('Hello!', 'What is your name?').result

# now display it briefly on the screen
droid.makeToast('Hello '+name)

# here display it in a dialog box
droid.dialogCreateAlert('Greetings!', 'Hello '+name)
droid.dialogSetPositiveButtonText('OK') 
droid.dialogShow()

Extending the Guessing Game Program

Take your guessing program and replace the raw_input and print statements with dialog boxes.

Next

Go back to the start.
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 12 Dec 2012 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated