下面列出了android.text.InputType#TYPE_NUMBER_FLAG_DECIMAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the input type for the listener.
*/
public int getInputType() {
int contentType;
if (mNeedsAdvancedInput) {
contentType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
} else {
contentType = InputType.TYPE_CLASS_NUMBER;
if (mSign) {
contentType |= InputType.TYPE_NUMBER_FLAG_SIGNED;
}
if (mDecimal) {
contentType |= InputType.TYPE_NUMBER_FLAG_DECIMAL;
}
}
return contentType;
}
@Override
public void setInputType(int type) {
if (type == -1) {
type = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_PASSWORD;
}
if (type == InputType.TYPE_CLASS_NUMBER ||
type == InputType.TYPE_NUMBER_FLAG_SIGNED ||
type == InputType.TYPE_NUMBER_FLAG_DECIMAL ||
type == InputType.TYPE_CLASS_PHONE) {
final String symbolExceptions = getSymbolExceptions();
this.setKeyListener(DigitsKeyListener.getInstance("0123456789." + symbolExceptions));
} else {
super.setInputType(type);
}
}
public int getInputType() {
int contentType = InputType.TYPE_CLASS_NUMBER;
if (mSign) {
contentType |= InputType.TYPE_NUMBER_FLAG_SIGNED;
}
if (mDecimal) {
contentType |= InputType.TYPE_NUMBER_FLAG_DECIMAL;
}
return contentType;
}
private static boolean validateEditText(EditText editText) {
boolean valid = true;
String text = editText.getText().toString();
boolean isEmail = (editText.getInputType() & InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS) == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
boolean isNumeric = (editText.getInputType() & InputType.TYPE_NUMBER_FLAG_DECIMAL) == InputType.TYPE_NUMBER_FLAG_DECIMAL;
if (TextUtils.isEmpty(text)) {
if (!isNumeric || !TextUtils.isDigitsOnly(editText.getHint())) {
valid = false;
}
} else if (isEmail) {
valid = android.util.Patterns.EMAIL_ADDRESS.matcher(text).matches();
}
if (!valid) {
Context context = editText.getContext();
if (isEmail) {
editText.setError(context.getString(R.string.error_invalid_email));
} else {
editText.setError(context.getString(R.string.error_blank));
}
return false;
}
editText.setError(null);
return true;
}
public void prepareFields(int index) {
TraitFormat traitFormat = traitFormats.getTraitFormatByIndex(index);
details.setHint(traitFormat.detailsBox().getParameterHint());
def.setHint(traitFormat.defaultBox().getParameterHint());
minimum.setHint(traitFormat.minimumBox().getParameterHint());
maximum.setHint(traitFormat.maximumBox().getParameterHint());
defBox.setVisibility(viewVisibility(traitFormat.isDefBoxVisible()));
def.setVisibility(viewVisibility(traitFormat.defaultBox().getParameterVisibility()));
minBox.setVisibility(viewVisibility(traitFormat.minimumBox().getParameterVisibility()));
maxBox.setVisibility(viewVisibility(traitFormat.maximumBox().getParameterVisibility()));
bool.setVisibility(viewVisibility(traitFormat.isBooleanVisible()));
categoryBox.setVisibility(viewVisibility(traitFormat.categoriesBox().getParameterVisibility()));
minimum.setText(traitFormat.minimumBox().getParameterDefaultValue());
maximum.setText(traitFormat.maximumBox().getParameterDefaultValue());
def.setText(traitFormat.defaultBox().getParameterDefaultValue());
if (traitFormat.isNumericInputType()) {
final int inputType = InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL;
def.setInputType(inputType);
minimum.setInputType(inputType);
maximum.setInputType(inputType);
}
}
@Test
public void testKeyboardType() {
ReactEditText view = mManager.createViewInstance(mThemedContext);
int numberPadTypeFlags = InputType.TYPE_CLASS_NUMBER;
int decimalPadTypeFlags = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL;
int numericTypeFlags =
InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL |
InputType.TYPE_NUMBER_FLAG_SIGNED;
int emailTypeFlags = InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS | InputType.TYPE_CLASS_TEXT;
int passwordVisibilityFlag = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD &
~InputType.TYPE_TEXT_VARIATION_PASSWORD;
int generalKeyboardTypeFlags = numericTypeFlags |
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS |
InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_PHONE |
passwordVisibilityFlag;
mManager.updateProperties(view, buildStyles());
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(InputType.TYPE_CLASS_TEXT);
mManager.updateProperties(view, buildStyles("keyboardType", "text"));
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(InputType.TYPE_CLASS_TEXT);
mManager.updateProperties(view, buildStyles("keyboardType", "number-pad"));
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(numberPadTypeFlags);
mManager.updateProperties(view, buildStyles("keyboardType", "decimal-pad"));
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(decimalPadTypeFlags);
mManager.updateProperties(view, buildStyles("keyboardType", "numeric"));
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(numericTypeFlags);
mManager.updateProperties(view, buildStyles("keyboardType", "email-address"));
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(emailTypeFlags);
mManager.updateProperties(view, buildStyles("keyboardType", "phone-pad"));
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(InputType.TYPE_CLASS_PHONE);
mManager.updateProperties(view, buildStyles("keyboardType", "visible-password"));
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(passwordVisibilityFlag);
mManager.updateProperties(view, buildStyles("keyboardType", null));
assertThat(view.getInputType() & generalKeyboardTypeFlags).isEqualTo(InputType.TYPE_CLASS_TEXT);
}
void setConstraints(int constraints) {
this.constraints = constraints;
if (textview != null) {
int inputtype;
switch (constraints & TextField.CONSTRAINT_MASK) {
default:
case TextField.ANY:
inputtype = InputType.TYPE_CLASS_TEXT;
break;
case TextField.EMAILADDR:
inputtype = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
break;
case TextField.NUMERIC:
inputtype = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED;
break;
case TextField.PHONENUMBER:
inputtype = InputType.TYPE_CLASS_PHONE;
break;
case TextField.URL:
inputtype = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI;
break;
case TextField.DECIMAL:
inputtype = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL;
break;
}
if ((constraints & TextField.PASSWORD) != 0 ||
(constraints & TextField.SENSITIVE) != 0) {
inputtype = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
}
if ((constraints & TextField.UNEDITABLE) != 0) {
inputtype = InputType.TYPE_NULL;
}
if ((constraints & TextField.NON_PREDICTIVE) != 0) {
inputtype |= InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
}
if ((constraints & TextField.INITIAL_CAPS_WORD) != 0) {
inputtype |= InputType.TYPE_TEXT_FLAG_CAP_WORDS;
}
if ((constraints & TextField.INITIAL_CAPS_SENTENCE) != 0) {
inputtype |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
}
textview.setInputType(inputtype);
if ((constraints & TextField.CONSTRAINT_MASK) == TextField.ANY) {
textview.setSingleLine(false);
}
}
}
private int getAndroidInputType(int codenameOneInputType, boolean multiline) {
int type = mInputTypeMap.get(codenameOneInputType, -1);
if (type == -1) {
if (!multiline && hasConstraint(codenameOneInputType, TextArea.NUMERIC)) {
type = InputType.TYPE_CLASS_NUMBER;
} else if (!multiline && hasConstraint(codenameOneInputType, TextArea.DECIMAL)) {
type = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED;
} else if (!multiline && hasConstraint(codenameOneInputType, TextArea.EMAILADDR)) {
type = makeNonPredictive(codenameOneInputType, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
} else if (hasConstraint(codenameOneInputType, TextArea.INITIAL_CAPS_SENTENCE)) {
type = makeNonPredictive(codenameOneInputType, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
} else if (hasConstraint(codenameOneInputType, TextArea.INITIAL_CAPS_WORD)) {
type = makeNonPredictive(codenameOneInputType, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
} else if (!multiline && hasConstraint(codenameOneInputType, TextArea.PASSWORD)) {
type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
} else if (!multiline && hasConstraint(codenameOneInputType, TextArea.PHONENUMBER)) {
type = makeNonPredictive(codenameOneInputType, InputType.TYPE_CLASS_PHONE);
} else if (!multiline && hasConstraint(codenameOneInputType, TextArea.URL)) {
type = makeNonPredictive(codenameOneInputType, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
} else {
type = makeNonPredictive(codenameOneInputType, InputType.TYPE_CLASS_TEXT);
}
}
// If we're editing standard text, disable auto complete.
// The name of the flag is a little misleading. From the docs:
// the text editor is performing auto-completion of the text being entered
// based on its own semantics, which it will present to the user as they type.
// This generally means that the input method should not be showing candidates itself,
// but can expect for the editor to supply its own completions/candidates from
// InputMethodSession.displayCompletions().
if ((type & InputType.TYPE_CLASS_TEXT) != 0 && (type & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) == 0) {
type |= InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE;
}
if (multiline) {
type |= InputType.TYPE_TEXT_FLAG_MULTI_LINE;
}
return type;
}