AI2 Component  (Version nb184)
TextBox.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
18 
19 import android.content.Context;
20 import android.text.InputType;
21 import android.view.inputmethod.EditorInfo;
22 import android.view.inputmethod.InputMethodManager;
23 import android.widget.EditText;
24 
53 @DesignerComponent(version = YaVersion.TEXTBOX_COMPONENT_VERSION,
54  description = "<p>A box for the user to enter text. The initial or " +
55  "user-entered text value is in the <code>Text</code> property. If " +
56  "blank, the <code>Hint</code> property, which appears as faint text " +
57  "in the box, can provide the user with guidance as to what to type.</p>" +
58  "<p>The <code>MultiLine</code> property determines if the text can have" +
59  "more than one line. For a single line text box, the keyboard will close" +
60  "automatically when the user presses the Done key. To close the keyboard " +
61  "for multiline text boxes, the app should use the HideKeyboard method or " +
62  " rely on the user to press the Back key.</p>" +
63  "<p>The <code> NumbersOnly</code> property restricts the keyboard to accept" +
64  "numeric input only.</p>" +
65  "<p>Other properties affect the appearance of the text box " +
66  "(<code>TextAlignment</code>, <code>BackgroundColor</code>, etc.) and " +
67  "whether it can be used (<code>Enabled</code>).</p>" +
68  "<p>Text boxes are usually used with the <code>Button</code> " +
69  "component, with the user clicking on the button when text entry is " +
70  "complete.</p>" +
71  "<p>If the text entered by the user should not be displayed, use " +
72  "<code>PasswordTextBox</code> instead.</p>",
73  category = ComponentCategory.USERINTERFACE)
74 @SimpleObject
75 public final class TextBox extends TextBoxBase {
76  /* TODO(user): this code requires Android SDK M5 or newer - we are currently on M3
77  enables this when we upgrade
78 
79  // Backing for text during validation
80  private String text;
81 
82  private class ValidationTransformationMethod extends TransformationMethod {
83  @Override
84  public CharSequence getTransformation(CharSequence source) {
85  BooleanReferenceParameter accept = new BooleanReferenceParameter(false);
86  Validate(source.toString, accept);
87 
88  if (accept.get()) {
89  text = source.toString();
90  }
91 
92  return text;
93  }
94  }
95 */
96 
97 
98  // If true, then accept numeric keyboard input only
99  private boolean acceptsNumbersOnly;
100 
101  // If true, then text box is multiline
102  private boolean multiLine;
103 
104  // If true, then text box is read-only
105  private boolean readOnly;
106 
113  super(container, new EditText(container.$context()));
114  NumbersOnly(false);
115  MultiLine(false);
116  ReadOnly(false);
117 
118  // We need to set the IME options here. Otherwise, Android's default
119  // behavior is that the action button will be Done or Next, depending on
120  // whether there is a next textbox on the screen, That might be convenient,
121  // but it seems a little obscure to be the standard behavior for
122  // App Inventor. Perhaps we could make that a property.
123  // This same line must be in to PasswordTextBox. We could have put it into
124  // TextBoxBase, but we might later be adding
125  // other flavors of text boxes that might want their own defaults.
126  view.setImeOptions(EditorInfo.IME_ACTION_DONE);
127  }
128 
129 
137  category = PropertyCategory.BEHAVIOR,
138  description = "If true, then this text box accepts only numbers as keyboard input. " +
139  "Numbers can include a decimal point and an optional leading minus sign. " +
140  "This applies to keyboard input only. Even if NumbersOnly is true, you " +
141  "can use [set Text to] to enter any text at all.")
142  public boolean NumbersOnly() {
143  return acceptsNumbersOnly;
144  }
145 
155  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "False")
157  description = "If true, then this text box accepts only numbers as keyboard input. " +
158  "Numbers can include a decimal point and an optional leading minus sign. " +
159  "This applies to keyboard input only. Even if NumbersOnly is true, you " +
160  "can use [set Text to] to enter any text at all.")
161  public void NumbersOnly(boolean acceptsNumbersOnly) {
162  if (acceptsNumbersOnly) {
163  view.setInputType(
164  InputType.TYPE_CLASS_NUMBER |
165  InputType.TYPE_NUMBER_FLAG_SIGNED |
166  InputType.TYPE_NUMBER_FLAG_DECIMAL);
167  } else {
168  view.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
169  }
170  this.acceptsNumbersOnly = acceptsNumbersOnly;
171  }
172 
178  description = "Hide the keyboard. Only multiline text boxes need this. " +
179  "Single line text boxes close the keyboard when the users presses the Done key.")
180  public void HideKeyboard() {
181  InputMethodManager imm =
182  (InputMethodManager) container.$context().getSystemService(Context.INPUT_METHOD_SERVICE);
183  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
184  }
185 
193  category = PropertyCategory.BEHAVIOR,
194  description = "If true, then this text box accepts multiple lines of input, which are " +
195  "entered using the return key. For single line text boxes there is a Done " +
196  "key instead of a return key, and pressing Done hides the keyboard. " +
197  "The app should call the HideKeyboard method to hide the keyboard for " +
198  "a mutiline text box.")
199  public boolean MultiLine() {
200  return multiLine;
201  }
202 
212  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "False")
213  @SimpleProperty()
214  public void MultiLine(boolean multiLine) {
215  this.multiLine = multiLine;
216  view.setSingleLine(!multiLine);
217  }
218 
220  category = PropertyCategory.BEHAVIOR,
221  description = "Whether the %type% is read-only. By default, this is true."
222  )
223  public boolean ReadOnly() {
224  return readOnly;
225  }
226 
233  defaultValue = "False"
234  )
236  public void ReadOnly(boolean readOnly) {
237  this.readOnly = readOnly;
238  view.setEnabled(!readOnly);
239  }
240 
241  // TODO(halabelson): We might also want a method to show the keyboard.
242  // Currently the text box keyboard will open when the text field becomes
243  // active, and that may be the best simple thing. If we implement show keyboard,
244  // note that showSoftInputFromWindow seems to open the keyboard only if it
245  // has been previously opened and closed.
246  }
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.TextBoxBase.view
final EditText view
Definition: TextBoxBase.java:40
com.google.appinventor.components.runtime.TextBox.TextBox
TextBox(ComponentContainer container)
Definition: TextBox.java:112
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components
com.google.appinventor.components.runtime.TextBox.MultiLine
boolean MultiLine()
Definition: TextBox.java:199
com.google.appinventor.components.runtime.TextBox.HideKeyboard
void HideKeyboard()
Definition: TextBox.java:180
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.TextBox.NumbersOnly
boolean NumbersOnly()
Definition: TextBox.java:142
com.google.appinventor.components.runtime.TextBox.ReadOnly
boolean ReadOnly()
Definition: TextBox.java:223
com.google.appinventor.components.runtime.TextBoxBase
Definition: TextBoxBase.java:37
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.TextBox.ReadOnly
void ReadOnly(boolean readOnly)
Definition: TextBox.java:236
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.TextBox
Definition: TextBox.java:75
com.google.appinventor.components.runtime.AndroidViewComponent.container
final ComponentContainer container
Definition: AndroidViewComponent.java:29
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor