Working with Textual Input Methods

Working with Textual Input Methods

The Android SDK includes input method framework classes that enable interested developers to use powerful input methods as well as create their own input methods, such as custom software keyboards and other Input Method Editors (IMEs). Users can download custom IMEs to use on their devices. For example, there’s nothing stopping a developer from creating a custom keyboard with Lord of the Rings-style Elvish characters, smiley faces, or Greek symbols. Tip Most device settings related to input methods are available under the Settings, Language & Keyboard menu. Here users can select the language as well as configure the custom user dictionary and make changes to how their keyboards function. The user can change the input method on the device by press-and-holding an EditText control, for example. A context menu comes up, allowing the user to change the input method (Android keyboard is usually the default).

Working with Software Keyboards

Because text input methods are locale-based (different countries use different alphabets and keyboards) and situational (numeric vs. alphabetic vs. special keys), the Android platform has trended toward software keyboards as opposed to relying on hardware manufacturers to deliver specialized hardware keyboards. Handling Advanced User Input 500 Chapter 24 Handling Advanced User Input Figure 24.

EditText

Controls with different input types.

Choosing the Appropriate Software Keyboard

The Android platform has a number of software keyboards available for use. One of the easiest ways to enable your users to enter data efficiently is to specify the type of input expected in each text input field. Tip Many of the code examples provided in this section are taken from the SimpleTextInputTypes application. The source code for this application is provided for download on the book website. For example, to specify an EditText that should take only capitalized textual input, you could set the inputType attribute as follows: Figure 24.1 shows a number of EditText controls with different inputType configurations. The input type dictates which software keyboard is used by default and it enforces appropriate rules, such as limiting input to certain characters. Working with Textual Input Methods 501 Figure 24.2 The software keyboards associated with specific input types. Figure 24.2 (left) illustrates what the software keyboard looks like for an EditText control with its inputType attribute set to all capitalized text input. Note how the software keyboard keys are all capitalized. If you were to set the inputType to textCapWords instead, the keyboard switches to lowercase after the first letter of each word and then back to uppercase after a space. Figure 24.2 (middle) illustrates what the software keyboard looks like for an EditText control with its inputType attribute set to number. Figure 24.2 (right) illustrates what the software keyboard looks like for an EditText control with its inputType attribute set to textual input, where each sentence begins with a capital letter and the text can be multiple lines. Depending on the user’s keyboard settings (specifically, if the user has enabled the Show Suggestions and Auto-complete options in the Android Keyboard settings of his device), the user might also see suggested words or spelling fixes while typing. For a complete list of inputType attribute values and their uses, see http://developer. android.com/reference/android/R.attr.html#inputType. Tip You can also have your Activity react to the display of software keyboards (to adjust where fields are displayed, for example) by requesting the WindowManager as a system service and modifying the layout parameters associated with the softInputMode field. 502 Chapter 24 Handling Advanced User Input For more fine-tuned control over input methods, see the android.view.inputmethod.InputMethodManager class.

Providing Custom Software Keyboards

If you are interested in developing your own software keyboards, we highly recommend the following references: n IMEs are implemented as an Android service. Begin by reviewing the Android packages called android.inputmethodservice and android.view.inputmethod, which can be used to implement custom input methods. n The SoftKeyboard sample application in the Android SDK provides an implementation of a software keyboard. n The Android Developer technical articles on onscreen input methods (http://developer.android.com/resources/articles/on-screen-inputs.html) and creating an input method (http://developer.android.com/resources/articles/ creating-input-method.html). Working with Text Prediction and User Dictionaries Text prediction is a powerful and flexible feature available on Android devices.We’ve already talked about many of these technologies in other parts of this book, but they merit mentioning in this context as well. n In Chapter 7,“Exploring User Interface Screen Elements,” you learned how to use AutoCompleteTextView and MultiAutoCompleteTextView controls to help users input common words and strings. n In Chapter 10,“Using Android Data and Storage APIs,” you learned how to tie an AutoCompleteTextView control to an underlying SQLite database table. n In Chapter 11,“Sharing Data Between Applications with Content Providers,” you learned about the UserDictionary content provider (android.provider.UserDictionary), which can be used to add words for the user’s custom dictionary of commonly used words. Exploring the Accessibility Framework The Android SDK includes numerous features and services for the benefit of users with visual and hearing impairments.Those users without such impairments also benefit from these features, especially when they are not paying complete attention to the device (such as when driving). Many of the most powerful accessibility features were added in Android 1.6 and 2.0, so check the API level for a specific class or method before using it within your application. Some of the accessibility features available within the Android SDK include n The Speech Recognition Framework. n The Text-To-Speech (TTS) Framework. Exploring the Accessibility Framework 503 n The ability to enable haptic feedback (that vibration you feel when you press a button, rather like a rumble pack game controller) on any View object (API Level 3 and higher). See the setHapticFeedbackEnabled() method of the View class. n The ability to set associated metadata, such as a text description of an ImageView control on any View object (API Level 4 and higher).This feature is often very helpful for the visually impaired. See the setContentDescription() method of the View class. n The ability to create and extend accessibility applications in conjunction with the Android Accessibility framework. See the following packages to get started writing accessibility applications: android.accessibilityservice and android.view.accessibility.There are also a number of accessibility applications, such as KickBack, SoundBack, and TalkBack, which ship with the platform. For more information, see the device settings under Settings,Accessibility. Tip Give some thought to providing accessibility features, such as providing View metadata, within your applications. There’s really no excuse for not doing so. Your users appreciate these small details, which make all the difference in terms of whether or not certain users can use your application at all. Also, make sure your quality assurance team verifies accessibility features as part of their testing process. Because speech recognition and Text-To-Speech applications are all the rage, and their technologies are often used for navigation applications (especially because many states are passing laws making driving while using a mobile device without hands-free operation illegal), let’s look at these two technologies in a little more detail. Android applications can leverage speech input and output. Speech input can be achieved using speech recognition services and speech output can be achieved using TextTo-Speech services. Not all devices support these services. However, certain types of applications—most notably hands-free applications such as directional navigation—often benefit from the use of these types of input. Speech services are available within the Android SDK in the android.speech package. The underlying services that make these technologies work might vary from device to device; some services might require a network connection to function properly. Tip Many of the code examples provided in this section are taken from the SimpleSpeech application. The source code for this application is provided for download on the book website. Speech services are best tested on a real Android device. We used an HTC Nexus One running Android 2.2 in our testing. Leveraging Speech Recognition Services You can enhance an application with speech recognition support by using the speech recognition framework provided within the Android SDK. Speech recognition involves speaking into the device microphone and enabling the software to detect and interpret 504 Chapter 24 Handling Advanced User Input Figure 24.3 Recording speech with the RecognizerIntent. that speech and translate it into a string. Speech recognition services are intended for use with short command-like phrases without pauses, not for long dictation. If you want more robust speech recognition, you need to implement your own solution. On Android SDK 2.1 and higher, access to speech recognition is built in to most popup keyboards.Therefore, an application might already support speech recognition, to some extent, without any changes. However, directly accessing the recognizer can allow for more interesting spoken-word control over applications. You can use the android.speech.RecognizerIntent intent to launch the built-in speech recorder.This launches the recorder (shown in Figure 24.3), allowing the user to record speech.

Formation et coursTélécharger le document complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *