下面列出了android.widget.EditText#getInputType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ReactTextInputLocalData(EditText editText) {
mText = new SpannableStringBuilder(editText.getText());
mTextSize = editText.getTextSize();
mInputType = editText.getInputType();
mPlaceholder = editText.getHint();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mMinLines = editText.getMinLines();
mMaxLines = editText.getMaxLines();
} else {
mMinLines = 1;
mMaxLines = 1;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mBreakStrategy = editText.getBreakStrategy();
} else {
mBreakStrategy = 0;
}
}
/**
* 设置密码是否可见
*/
public static void passwordVisibleToggle(@NonNull EditText editText) {
if (editText.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
editText.setInputType(
InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
} else {
editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}
}
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;
}
private int getPasswordMask(EditText maskedField) {
int inputType = maskedField.getInputType();
int maskedFieldPasswordMask = (inputType & InputType.TYPE_TEXT_VARIATION_PASSWORD
| inputType & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
maskedFieldPasswordMask |= inputType & InputType.TYPE_NUMBER_VARIATION_PASSWORD;
maskedFieldPasswordMask |= inputType & InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD;
}
return maskedFieldPasswordMask;
}
@OnTouch({ R.id.hour, R.id.minute, R.id.second })
boolean switchField(EditText field, MotionEvent event) {
int inType = field.getInputType(); // backup the input type
field.setInputType(InputType.TYPE_NULL); // disable soft input
boolean result = field.onTouchEvent(event); // call native handler
field.setInputType(inType); // restore input type (to show cursor)
return result;
}
private static boolean isInputTypePassword(EditText editText) {
return editText != null
&& (editText.getInputType() == InputType.TYPE_NUMBER_VARIATION_PASSWORD
|| editText.getInputType() == InputType.TYPE_TEXT_VARIATION_PASSWORD
|| editText.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
|| editText.getInputType() == InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD);
}
/**
* 设置密码是否可见
*/
public static void passwordVisibleToggle(EditText editText) {
Log.d("input", "passwordVisibleToggle " + editText.getInputType());
if (editText.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
} else {
editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}
}
@Test
public void testSuggestionsListDisablesSpellingSuggestions() throws Exception {
EditText input = TestUtils.getPrivateField(mRichEditor, "mMentionsEditText");
int originalInputType = input.getInputType();
mRichEditor.displaySuggestions(true);
// should be no suggestions
assertEquals(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS, input.getInputType());
mRichEditor.displaySuggestions(false);
// should be back to original type
assertEquals(originalInputType, input.getInputType());
}
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
mText = (EditText) findViewById(R.id.text);
if (mText != null)
{
mText.addTextChangedListener(this);
int inputType = mText.getInputType();
mText.setInputType(inputType | InputType.TYPE_TEXT_VARIATION_URI);
}
}
/**
* 设置密码是否可见
*/
public static void passwordVisibleToggle(EditText editText) {
if (editText.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
editText.setInputType(
InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
} else {
editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if(preference instanceof EditTextPreference && newValue instanceof String) {
EditText prefText = ((EditTextPreference) preference).getEditText();
int editorType = prefText.getInputType();
if(EditorInfo.TYPE_CLASS_NUMBER == (EditorInfo.TYPE_CLASS_NUMBER & editorType)) {
// Try to convert to double
double res;
try {
res = Double.valueOf((String)newValue);
} catch (NumberFormatException ex) {
return false;
}
if(EditorInfo.TYPE_NUMBER_FLAG_SIGNED != (EditorInfo.TYPE_NUMBER_FLAG_SIGNED & editorType)) {
// Must be superior than 0
if(res < 0) {
return false;
}
}
if(EditorInfo.TYPE_NUMBER_FLAG_SIGNED != (EditorInfo.TYPE_NUMBER_FLAG_SIGNED & editorType)) {
// Must be an integer
if(Double.compare(res % 1,0) != 0) {
return false;
}
}
}
return true;
}
return true;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v instanceof EditText) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
edittext.setSelection(edittext.getText().length());
return true;
}
return false;
}
private static void setTextEntryKeyboardAction(EditText textEntry, int action) {
// bug/feature that requires setting the input type to null then changing the action type
int inputType = textEntry.getInputType();
textEntry.setInputType(InputType.TYPE_NULL);
textEntry.setImeOptions(action);
textEntry.setInputType(inputType);
}
public static boolean isTextInput(EditText editText) {
return (editText.getInputType() & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT;
}
public static boolean isPhoneInput(EditText editText) {
return (editText.getInputType() & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_PHONE;
}
static boolean isTextInput(EditText editText) {
return (editText.getInputType() & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT;
}
static boolean isPhoneInput(EditText editText) {
return (editText.getInputType() & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_PHONE;
}
private static void setInputTypeIfChanged(EditText editText, int inputType) {
// Avoid redundant call to InputMethodManager#restartInput.
if (inputType != editText.getInputType()) {
editText.setInputType(inputType);
}
}
public static boolean isPhoneInput(EditText editText) {
return (editText.getInputType() & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_PHONE;
}
@Override
protected boolean matchesSafely(EditText view) {
return view.getInputType() == inputType;
}