下面列出了android.view.inputmethod.ExtractedTextRequest#android.view.inputmethod.EditorInfo 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setUpNumberInput() {
EditText numberInput = number.getInput();
numberInput.addTextChangedListener(new NumberChangedListener());
number.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus) {
scrollView.postDelayed(() -> scrollView.smoothScrollTo(0, register.getBottom()), 250);
}
});
numberInput.setImeOptions(EditorInfo.IME_ACTION_DONE);
numberInput.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
hideKeyboard(requireContext(), v);
handleRegister(requireContext());
return true;
}
return false;
});
}
public void loadKeyboard(final EditorInfo editorInfo, final SettingsValues settingsValues,
final int currentAutoCapsState, final int currentRecapitalizeState) {
final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
mThemeContext, editorInfo);
final Resources res = mThemeContext.getResources();
final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
final int keyboardHeight = ResourceUtils.getKeyboardHeight(res, settingsValues);
builder.setKeyboardGeometry(keyboardWidth, keyboardHeight);
builder.setSubtype(mRichImm.getCurrentSubtype());
builder.setVoiceInputKeyEnabled(settingsValues.mShowsVoiceInputKey);
builder.setLanguageSwitchKeyEnabled(mLatinIME.shouldShowLanguageSwitchKey());
builder.setSplitLayoutEnabledByUser(ProductionFlags.IS_SPLIT_KEYBOARD_SUPPORTED
&& settingsValues.mIsSplitKeyboardEnabled);
mKeyboardLayoutSet = builder.build();
try {
mState.onLoadKeyboard(currentAutoCapsState, currentRecapitalizeState);
mKeyboardTextsSet.setLocale(mRichImm.getCurrentSubtypeLocale(), mThemeContext);
} catch (KeyboardLayoutSetException e) {
Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause());
}
}
public void setTransport(TransportOption transport) {
final boolean useSystemEmoji = TextSecurePreferences.isSystemEmojiPreferred(getContext());
final boolean isIncognito = TextSecurePreferences.isIncognitoKeyboardEnabled(getContext());
int imeOptions = (getImeOptions() & ~EditorInfo.IME_MASK_ACTION) | EditorInfo.IME_ACTION_SEND;
int inputType = getInputType();
if (isLandscape()) setImeActionLabel(transport.getComposeHint(), EditorInfo.IME_ACTION_SEND);
else setImeActionLabel(null, 0);
if (useSystemEmoji) {
inputType = (inputType & ~InputType.TYPE_MASK_VARIATION) | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE;
}
setInputType(inputType);
setImeOptions(imeOptions);
setHint(transport.getComposeHint(),
transport.getSimName().isPresent()
? getContext().getString(R.string.conversation_activity__from_sim_name, transport.getSimName().get())
: null);
}
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
String newEnergyExpendedString = textView.getText().toString();
if (isValidCharacteristicValue(newEnergyExpendedString,
EXPENDED_ENERGY_FORMAT)) {
int newEnergyExpended = Integer.parseInt(newEnergyExpendedString);
mHeartRateMeasurementCharacteristic.setValue(newEnergyExpended,
EXPENDED_ENERGY_FORMAT,
/* offset */ 2);
} else {
Toast.makeText(getActivity(), R.string.energyExpendedInvalid,
Toast.LENGTH_SHORT).show();
}
}
return false;
}
private void initializeResources() {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.centered_app_title);
ImageButton okButton = (ImageButton) findViewById(R.id.ok_button);
passphraseText = (EditText) findViewById(R.id.passphrase_edit);
SpannableString hint = new SpannableString(" " + getString(R.string.PassphrasePromptActivity_enter_passphrase));
hint.setSpan(new RelativeSizeSpan(0.9f), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
hint.setSpan(new TypefaceSpan("sans-serif"), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
passphraseText.setHint(hint);
okButton.setOnClickListener(new OkButtonClickListener());
passphraseText.setOnEditorActionListener(new PassphraseActionListener());
passphraseText.setImeActionLabel(getString(R.string.prompt_passphrase_activity__unlock),
EditorInfo.IME_ACTION_DONE);
}
private void init() {
mTokenPattern = Pattern.compile("[^,]+");
setOnItemClickListener(this);
addTextChangedListener(mTextWatcher);
// regenerate chips when user taps "Done" action on the keyboard
setOnEditorActionListener((view, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
updateChips();
// don't consume the event, so the keyboard can also be hidden
// http://stackoverflow.com/questions/2342620/how-to-hide-keyboard-after-typing-in-edittext-in-android#comment20849208_10184099
return false;
}
return false;
});
}
public EditTextSettingsCell(Context context) {
super(context);
textView = new EditTextBoldCursor(context);
textView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
textView.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL);
textView.setBackgroundDrawable(null);
textView.setPadding(0, 0, 0, 0);
textView.setInputType(textView.getInputType() | EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES);
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, 21, 0, 21, 0));
}
public EditTextSettingsCell(Context context) {
super(context);
textView = new EditText(context);
textView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
textView.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL);
textView.setBackgroundDrawable(null);
textView.setPadding(0, 0, 0, 0);
textView.setInputType(textView.getInputType() |EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES);
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, 17, 0, 17, 0));
}
/**
* Ask the input target to execute its default action via
* {@link InputConnection#performEditorAction
* InputConnection.performEditorAction()}.
*
* @param fromEnterKey If true, this will be executed as if the user had
* pressed an enter key on the keyboard, that is it will <em>not</em>
* be done if the editor has set {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION
* EditorInfo.IME_FLAG_NO_ENTER_ACTION}. If false, the action will be
* sent regardless of how the editor has set that flag.
*
* @return Returns a boolean indicating whether an action has been sent.
* If false, either the editor did not specify a default action or it
* does not want an action from the enter key. If true, the action was
* sent (or there was no input connection at all).
*/
public boolean sendDefaultEditorAction(boolean fromEnterKey) {
EditorInfo ei = getCurrentInputEditorInfo();
if (ei != null &&
(!fromEnterKey || (ei.imeOptions &
EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) &&
(ei.imeOptions & EditorInfo.IME_MASK_ACTION) !=
EditorInfo.IME_ACTION_NONE) {
// If the enter key was pressed, and the editor has a default
// action associated with pressing enter, then send it that
// explicit action instead of the key event.
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.performEditorAction(ei.imeOptions&EditorInfo.IME_MASK_ACTION);
}
return true;
}
return false;
}
/**
* Return a drawable resource id that can be used as a button icon for the given
* {@link EditorInfo#imeOptions EditorInfo.imeOptions}.
*
* @param imeOptions The value from @link EditorInfo#imeOptions EditorInfo.imeOptions}.
*
* @return Returns a drawable resource id to use.
*/
@DrawableRes
private int getIconForImeAction(int imeOptions) {
switch (imeOptions&EditorInfo.IME_MASK_ACTION) {
case EditorInfo.IME_ACTION_GO:
return com.android.internal.R.drawable.ic_input_extract_action_go;
case EditorInfo.IME_ACTION_SEARCH:
return com.android.internal.R.drawable.ic_input_extract_action_search;
case EditorInfo.IME_ACTION_SEND:
return com.android.internal.R.drawable.ic_input_extract_action_send;
case EditorInfo.IME_ACTION_NEXT:
return com.android.internal.R.drawable.ic_input_extract_action_next;
case EditorInfo.IME_ACTION_DONE:
return com.android.internal.R.drawable.ic_input_extract_action_done;
case EditorInfo.IME_ACTION_PREVIOUS:
return com.android.internal.R.drawable.ic_input_extract_action_previous;
default:
return com.android.internal.R.drawable.ic_input_extract_action_return;
}
}
@OnCheckedChanged(R2.id.companyAccount)
protected void onCompanyCheckedChange(boolean isChecked) {
if (isChecked) {
int backgroundDefault = ContextCompat.getColor(CreateAccountActivity.this,
R.color.register_field_background_color_enabled);
verifyPasswordView.setImeOptions(EditorInfo.IME_ACTION_NEXT);
companyNameView.setBackgroundColor(backgroundDefault);
companyChoiceView.setText(R.string.prompt_company_account_enabled);
} else {
verifyPasswordView.setImeOptions(EditorInfo.IME_ACTION_DONE);
companyNameView.setBackgroundColor(ContextCompat.getColor(CreateAccountActivity.this,
R.color.register_field_background_color_disabled));
companyChoiceView.setText(R.string.prompt_company_account_disabled);
}
companyNameView.setEnabled(isChecked);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
final InputConnection ic = super.onCreateInputConnection(outAttrs);
if (ic != null && outAttrs.hintText == null) {
// If we don't have a hint and our parent is a TextInputLayout, use it's hint for the
// EditorInfo. This allows us to display a hint in 'extract mode'.
ViewParent parent = getParent();
while (parent instanceof View) {
if (parent instanceof TextInputLayout) {
outAttrs.hintText = ((TextInputLayout) parent).getHint();
break;
}
parent = parent.getParent();
}
}
return ic;
}
private void initViews() {
getVerifiCodeButton = getView(R.id.btn_send_verifi_code);
getVerifiCodeButton.setOnClickListener(this);
phoneEdit = getView(R.id.et_phone);
phoneEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);// 下一步
verifyCodeEdit = getView(R.id.et_verifiCode);
verifyCodeEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);// 下一步
passwordEdit = getView(R.id.et_password);
passwordEdit.setImeOptions(EditorInfo.IME_ACTION_DONE);
passwordEdit.setImeOptions(EditorInfo.IME_ACTION_GO);
passwordEdit.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// 点击虚拟键盘的done
if (actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_GO) {
commit();
}
return false;
}
});
}
@Override
protected void setUpView(View view) {
mRootView = view;
// from, to, amount edit text
AutoCompleteTextView etFrom = view.findViewById(R.id.et_from);
AutoCompleteTextView etTo = view.findViewById(R.id.et_to);
EditText etAmount = view.findViewById(R.id.et_amount);
// click handler
view.findViewById(R.id.btn_transfer).setOnClickListener(v -> onSend() );
etAmount.setOnEditorActionListener((textView, actionId, keyEvent) -> {
if (EditorInfo.IME_ACTION_SEND == actionId) {
onSend();
return true;
}
return false;
});
// account history
UiUtils.setupAccountHistory( etFrom, etTo );
}
public void loadKeyboard(final EditorInfo editorInfo, final SettingsValues settingsValues,
final int currentAutoCapsState, final int currentRecapitalizeState) {
final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
mThemeContext, editorInfo);
final Resources res = mThemeContext.getResources();
final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
final int keyboardHeight = ResourceUtils.getKeyboardHeight(res, settingsValues);
builder.setKeyboardGeometry(keyboardWidth, keyboardHeight);
builder.setSubtype(mRichImm.getCurrentSubtype());
builder.setVoiceInputKeyEnabled(settingsValues.mShowsVoiceInputKey);
builder.setLanguageSwitchKeyEnabled(mLatinIME.shouldShowLanguageSwitchKey());
builder.setEmojiSwitchKeyEnabled(mLatinIME.shouldShowEmojiSwitchKey());
builder.setNumberRowEnabled(mLatinIME.shouldShowNumberRow());
builder.setSplitLayoutEnabledByUser(ProductionFlags.IS_SPLIT_KEYBOARD_SUPPORTED
&& settingsValues.mIsSplitKeyboardEnabled);
mKeyboardLayoutSet = builder.build();
try {
mState.onLoadKeyboard(currentAutoCapsState, currentRecapitalizeState);
mKeyboardTextsSet.setLocale(mRichImm.getCurrentSubtypeLocale(), mThemeContext);
} catch (KeyboardLayoutSetException e) {
Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause());
}
}
void onStartInputInternal(final EditorInfo editorInfo, final boolean restarting) {
super.onStartInput(editorInfo, restarting);
// If the primary hint language does not match the current subtype language, then try
// to switch to the primary hint language.
// TODO: Support all the locales in EditorInfo#hintLocales.
final Locale primaryHintLocale = EditorInfoCompatUtils.getPrimaryHintLocale(editorInfo);
if (primaryHintLocale == null) {
return;
}
final InputMethodSubtype newSubtype = mRichImm.findSubtypeByLocale(primaryHintLocale);
if (newSubtype == null || newSubtype.equals(mRichImm.getCurrentSubtype().getRawSubtype())) {
return;
}
mHandler.postSwitchLanguage(newSubtype);
}
/**
* This method is used to set layout for login screen and handle login functionalities accordingly
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getClient().user().isUserLoggedIn()){
toMainActivity();
}
setContentView(R.layout.activity_login);
defineView();
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
}
@Override
public InputConnection onCreateInputConnection(final EditorInfo info) {
final InputConnection ic = super.onCreateInputConnection(info);
EditorInfoCompat.setContentMimeTypes(info, new String[]{"image/gif"});
final InputConnectionCompat.OnCommitContentListener callback = (info1, flags, opts) -> {
if (BuildVersionUtil.isAtLeastNMR1() && (flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
try {
info1.requestPermission();
} catch (Exception e) {
return false;
}
}
if (commitListener != null) {
commitListener.onCommitContent(info1);
}
return true;
};
return InputConnectionCompat.createWrapper(ic, info, callback);
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (getView() == null || actionId != EditorInfo.IME_ACTION_DONE) {
return false;
}
switch (v.getId()) {
case R.id.service_name:
regTypeEditText.requestFocus();
return true;
case R.id.reg_type:
portEditText.requestFocus();
return true;
case R.id.port:
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
return true;
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mPreviousInputType = passwordET.getInputType();
setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD, true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
setInputType(mPreviousInputType, true);
mPreviousInputType = -1;
break;
}
return false;
}
/**
* Returns whether the device should obscure typed password characters.
* Typically this means speaking "dot" in place of non-control characters.
*
* @return {@code true} if the device should obscure password characters.
*/
@SuppressWarnings("deprecation")
public boolean shouldObscureInput(final EditorInfo editorInfo) {
if (editorInfo == null) return false;
// The user can optionally force speaking passwords.
if (SettingsSecureCompatUtils.ACCESSIBILITY_SPEAK_PASSWORD != null) {
final boolean speakPassword = Settings.Secure.getInt(mContext.getContentResolver(),
SettingsSecureCompatUtils.ACCESSIBILITY_SPEAK_PASSWORD, 0) != 0;
if (speakPassword) return false;
}
// Always speak if the user is listening through headphones.
if (mAudioManager.isWiredHeadsetOn() || mAudioManager.isBluetoothA2dpOn()) {
return false;
}
// Don't speak if the IME is connected to a password field.
return InputTypeUtils.isPasswordInputType(editorInfo.inputType);
}
public void testActionSend() {
final ExpectedActionKey expectedKey = ExpectedActionKey.newIconKey(
KeyboardIconsSet.NAME_SEND_KEY);
for (final InputMethodSubtype subtype : getAllSubtypesList()) {
final String tag = "send " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
doTestActionKey(tag, subtype, EditorInfo.IME_ACTION_SEND, expectedKey);
}
}
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
// If the action is a key-up event on the return key, send the message
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
String message = view.getText().toString();
sendMessage(message);
}
return true;
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
checkPassword(v.getText().toString());
return true;
}
return false;
}
private void initListener() {
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
mLoginPresenter.login();
return true;
}
return false;
}
});
}
private void loadSettings() {
final Locale locale = mRichImm.getCurrentSubtypeLocale();
final EditorInfo editorInfo = getCurrentInputEditorInfo();
final InputAttributes inputAttributes = new InputAttributes(editorInfo, isFullscreenMode());
mSettings.loadSettings(this, locale, inputAttributes);
final SettingsValues currentSettingsValues = mSettings.getCurrent();
AudioAndHapticFeedbackManager.getInstance().onSettingsChanged(currentSettingsValues);
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
onAddPrinterButtonClicked();
}
return false;
}
@Override
protected KeyboardLayoutSet createKeyboardLayoutSet(final InputMethodSubtype subtype,
final EditorInfo editorInfo, final boolean voiceInputKeyEnabled,
final boolean languageSwitchKeyEnabled, final boolean splitLayoutEnabled) {
final EditorInfo emailField = new EditorInfo();
emailField.inputType =
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI;
return super.createKeyboardLayoutSet(
subtype, emailField, voiceInputKeyEnabled, languageSwitchKeyEnabled,
splitLayoutEnabled);
}
@Override
public boolean onEditorAction(final TextView textView, final int actionId, final KeyEvent keyEvent) {
if (D) Log.d(TAG, "mTxtCommand_OnEditorActionListener.onEditorAction()");
//handle the the virtual keyboard send button or physical keyboard action/enter button
if (actionId == EditorInfo.IME_ACTION_SEND || (actionId == EditorInfo.IME_NULL && keyEvent.getAction() == KeyEvent.ACTION_DOWN)) {
sendCommand();
return true;
}
return false;
}
public static int getImeOptionsActionIdFromEditorInfo(final EditorInfo editorInfo) {
if ((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
return EditorInfo.IME_ACTION_NONE;
} else if (editorInfo.actionLabel != null) {
return IME_ACTION_CUSTOM_LABEL;
} else {
// Note: this is different from editorInfo.actionId, hence "ImeOptionsActionId"
return editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;
}
}