下面列出了android.widget.TextView#OnEditorActionListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected final void addEditorActionListener(TextView.OnEditorActionListener listener) {
TextView view;
if (listener != null && (view = getHostView()) != null) {
if (mEditorActionListeners == null) {
mEditorActionListeners = new ArrayList<>();
view.setOnEditorActionListener(new TextView.OnEditorActionListener() {
private boolean handled = true;
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
for (TextView.OnEditorActionListener l : mEditorActionListeners) {
if (l != null) {
handled = handled & l.onEditorAction(v, actionId, event);
}
}
return handled;
}
});
}
mEditorActionListeners.add(listener);
}
}
public static TextView.OnEditorActionListener getListenerRunOnEnter(final Runnable r) {
return new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_SEND ||
isEnterKeyDown(event)) {
if (event == null || !event.isShiftPressed()) {
r.run(); // The user is done typing.
return true; // Consume.
}
}
return false; // Pass on to other listeners.
}
};
}
/**
* Makes an OnEditorActionListener that calls filtersAdd when done is pressed
*
* @param filtersAdd called when done is pressed
* @return The new OnEditorActionListener
*/
private TextView.OnEditorActionListener makeOnEditorActionListener(Consumer<String> filtersAdd) {
return new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
String text = v.getText().toString().toLowerCase(Locale.ENGLISH).trim();
if (!text.isEmpty()) {
filtersAdd.accept(text);
v.setText("");
updateFilters();
}
}
return false;
}
};
}
public static void showSearchDialog (Context context, int titleRes, final OnSearchListener listener) {
final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
final EditText editText = (EditText) LayoutInflater.from(context).inflate(R.layout.dialog_editbox, null);
TextView.OnEditorActionListener onEnter = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
performSearch(editText, listener);
return true;
}
};
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
editText.setOnEditorActionListener(onEnter);
alertBuilder.setView(editText);
alertBuilder.setTitle(titleRes);
alertBuilder.setPositiveButton(R.string.action_search, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
performSearch(editText, listener);
}
});
alertBuilder.setNegativeButton(R.string.dialog_cancel, null);
final AlertDialog alertDialog = alertBuilder.create();
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
alertDialog.show();
}
public void setOnEditorActionListener(TextView.OnEditorActionListener actionListener) {
editText.setOnEditorActionListener((v, actionId, event) -> {
if (validate())
return actionListener.onEditorAction(v, actionId, event);
return true;
});
}
public static TextView.OnEditorActionListener getListenerRunOnEnter(final Runnable r) {
return (v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_SEND ||
isEnterKeyDown(event)) {
if (event == null || !event.isShiftPressed()) {
r.run(); // The user is done typing.
return true; // Consume.
}
}
return false; // Pass on to other listeners.
};
}
/**
* Facilitates binding editor action listeners on EditText fields.
*/
@BindingAdapter("onEditorAction")
public static void setOnEditorActionListener(
EditText layout,
TextView.OnEditorActionListener listener) {
layout.setOnEditorActionListener(listener);
}
/**
* Builds the editor view.
*
* @param activity The activity on top of which the UI should be displayed.
* @param observerForTest Optional event observer for testing.
*/
public EditorView(Activity activity, PaymentRequestObserverForTest observerForTest) {
super(activity, R.style.FullscreenWhiteDialog);
mContext = activity;
mObserverForTest = observerForTest;
mHandler = new Handler();
mPhoneFormatterTask = new AsyncTask<Void, Void, PhoneNumberFormattingTextWatcher>() {
@Override
protected PhoneNumberFormattingTextWatcher doInBackground(Void... unused) {
return new PhoneNumberFormattingTextWatcher();
}
}.execute();
mEditorActionListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
mDoneButton.performClick();
return true;
} else if (actionId == EditorInfo.IME_ACTION_NEXT) {
View next = v.focusSearch(View.FOCUS_FORWARD);
if (next != null && next instanceof AutoCompleteTextView) {
focusInputField(next);
return true;
}
}
return false;
}
};
}
/**
* Change the custom IME action associated with the text view. click the lable will trigger the associateView's onClick method
* @param listener
*/
public T imeAction(TextView.OnEditorActionListener listener) {
if (view instanceof EditText) {
((EditText)view).setOnEditorActionListener(listener);
}
return self();
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setAppNameTitle();
setContentView(R.layout.activity_watchonly);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setTitleBackTransparent();
setAppNameTitle();
mUsernameText = UI.find(this, R.id.input_user);
mPasswordText = UI.find(this, R.id.input_password);
mLoginButton = UI.find(this, R.id.btn_login);
mRememberSwitch = UI.find(this, R.id.remember_watch_only);
mLoginButton.setOnClickListener(this);
final TextView.OnEditorActionListener listener;
listener = UI.getListenerRunOnEnter(this::onLoginButtonClicked);
mPasswordText.setOnEditorActionListener(listener);
final String username = cfg().getString(PrefKeys.WATCH_ONLY_USERNAME, "");
final String password = cfg().getString(PrefKeys.WATCH_ONLY_PASSWORD, "");
final boolean hasCredentials = !username.isEmpty();
mUsernameText.setText(username);
mPasswordText.setText(password);
mRememberSwitch.setChecked(hasCredentials);
mRememberSwitch.setOnCheckedChangeListener((compoundButton, isChecked) -> {
if (!isChecked) {
cfg().edit().putString(PrefKeys.WATCH_ONLY_USERNAME, "")
.putString(PrefKeys.WATCH_ONLY_PASSWORD, "").apply();
mUsernameText.setText("");
mPasswordText.setText("");
return;
}
UI.popup(WatchOnlyLoginActivity.this, R.string.id_warning_watchonly_credentials)
.content(R.string.id_your_watchonly_username_and)
.canceledOnTouchOutside(false)
.onNegative((dlg, which) -> mRememberSwitch.setChecked(false))
.onPositive((dlg, which) -> mRememberSwitch.setChecked(true)).build().show();
});
}
@OnMount
static void onMount(
final ComponentContext c,
EditTextWithEventHandlers editText,
@Prop(optional = true, resType = ResType.STRING) CharSequence text,
@Prop(optional = true, resType = ResType.STRING) CharSequence initialText,
@Prop(optional = true, resType = ResType.STRING) CharSequence hint,
@Prop(optional = true) TextUtils.TruncateAt ellipsize,
@Prop(optional = true, resType = ResType.INT) int minLines,
@Prop(optional = true, resType = ResType.INT) int maxLines,
@Prop(optional = true, resType = ResType.INT) int maxLength,
@Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowRadius,
@Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDx,
@Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDy,
@Prop(optional = true, resType = ResType.COLOR) int shadowColor,
@Prop(optional = true, resType = ResType.BOOL) boolean isSingleLine,
@Prop(optional = true, resType = ResType.COLOR) int textColor,
@Prop(optional = true) ColorStateList textColorStateList,
@Prop(optional = true, resType = ResType.COLOR) int hintColor,
@Prop(optional = true) ColorStateList hintColorStateList,
@Prop(optional = true, resType = ResType.COLOR) int linkColor,
@Prop(optional = true, resType = ResType.COLOR) int highlightColor,
@Prop(optional = true) ColorStateList tintColorStateList,
@Prop(optional = true, resType = ResType.DIMEN_TEXT) int textSize,
@Prop(optional = true, resType = ResType.DIMEN_OFFSET) float extraSpacing,
@Prop(optional = true, resType = ResType.FLOAT) float spacingMultiplier,
@Prop(optional = true) int textStyle,
@Prop(optional = true) Typeface typeface,
@Prop(optional = true) Layout.Alignment textAlignment,
@Prop(optional = true) int gravity,
@Prop(optional = true) boolean editable,
@Prop(optional = true) int selection,
@Prop(optional = true) int inputType,
@Prop(optional = true) int rawInputType,
@Prop(optional = true) int imeOptions,
@Prop(optional = true) TextView.OnEditorActionListener editorActionListener,
@Prop(optional = true) boolean isSingleLineWrap,
@Prop(optional = true) boolean requestFocus,
@Prop(optional = true) int cursorDrawableRes,
@Prop(optional = true, varArg = "inputFilter") List<InputFilter> inputFilters,
@State AtomicReference<EditTextWithEventHandlers> mountedView,
@State AtomicBoolean configuredInitialText,
@State(canUpdateLazily = true) CharSequence input) {
mountedView.set(editText);
initEditText(
editText,
input == null ? text : input,
// Only set initialText on the EditText during the very first mount.
configuredInitialText.getAndSet(true) ? null : initialText,
hint,
ellipsize,
inputFilters,
minLines,
maxLines,
maxLength,
shadowRadius,
shadowDx,
shadowDy,
shadowColor,
isSingleLine,
textColor,
textColorStateList,
hintColor,
hintColorStateList,
linkColor,
highlightColor,
tintColorStateList,
textSize,
extraSpacing,
spacingMultiplier,
textStyle,
typeface,
textAlignment,
gravity,
editable,
selection,
inputType,
rawInputType,
imeOptions,
editorActionListener,
isSingleLineWrap,
requestFocus,
cursorDrawableRes);
}
/**
* Builds the editor view.
*
* @param activity The activity on top of which the UI should be displayed.
* @param observerForTest Optional event observer for testing.
*/
public EditorView(Activity activity, PaymentRequestObserverForTest observerForTest) {
super(activity, R.style.FullscreenWhite);
mContext = activity;
mObserverForTest = observerForTest;
mHandler = new Handler();
mEditorActionListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
mDoneButton.performClick();
return true;
} else if (actionId == EditorInfo.IME_ACTION_NEXT) {
View next = v.focusSearch(View.FOCUS_FORWARD);
if (next != null) {
next.requestFocus();
return true;
}
}
return false;
}
};
mHalfRowMargin = activity.getResources().getDimensionPixelSize(
R.dimen.payments_section_large_spacing);
mFieldViews = new ArrayList<>();
mEditableTextFields = new ArrayList<>();
mDropdownFields = new ArrayList<>();
final Pattern cardNumberPattern = Pattern.compile("^[\\d- ]*$");
mCardNumberInputFilter = new InputFilter() {
@Override
public CharSequence filter(
CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// Accept deletions.
if (start == end) return null;
// Accept digits, "-", and spaces.
if (cardNumberPattern.matcher(source.subSequence(start, end)).matches()) {
return null;
}
// Reject everything else.
return "";
}
};
mCardNumberFormatter = new CreditCardNumberFormattingTextWatcher();
new AsyncTask<Void, Void, PhoneNumberFormattingTextWatcher>() {
@Override
protected PhoneNumberFormattingTextWatcher doInBackground(Void... unused) {
return new PhoneNumberFormattingTextWatcher();
}
@Override
protected void onPostExecute(PhoneNumberFormattingTextWatcher result) {
mPhoneFormatter = result;
if (mPhoneInput != null) {
mPhoneInput.addTextChangedListener(mPhoneFormatter);
}
}
}.execute();
}
public void setOnEditorActionListener(TextView.OnEditorActionListener l) {
if (innerEditText != null) {
innerEditText.setOnEditorActionListener(l);
}
}
public void setOnEditorActionListener(TextView.OnEditorActionListener actionListener) {
binding.editSearch.setOnEditorActionListener(actionListener);
}
public void setOnEditorActionListener(TextView.OnEditorActionListener onEditorActionListener) {
this.displayEditText.setOnEditorActionListener(onEditorActionListener);
}
/**
* Builds the editor dialog.
*
* @param activity The activity on top of which the UI should be displayed.
* @param observerForTest Optional event observer for testing.
*/
public EditorDialog(Activity activity, PaymentRequestObserverForTest observerForTest) {
super(activity, R.style.FullscreenWhite);
// Sets transparent background for animating content view.
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mContext = activity;
mObserverForTest = observerForTest;
mHandler = new Handler();
mEditorActionListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
mDoneButton.performClick();
return true;
} else if (actionId == EditorInfo.IME_ACTION_NEXT) {
View next = v.focusSearch(View.FOCUS_FORWARD);
if (next != null) {
next.requestFocus();
return true;
}
}
return false;
}
};
mHalfRowMargin = activity.getResources().getDimensionPixelSize(
R.dimen.payments_section_large_spacing);
mDropdownTopPadding = activity.getResources().getDimensionPixelSize(
R.dimen.payments_section_dropdown_top_padding);
mFieldViews = new ArrayList<>();
mEditableTextFields = new ArrayList<>();
mDropdownFields = new ArrayList<>();
final Pattern cardNumberPattern = Pattern.compile("^[\\d- ]*$");
mCardNumberInputFilter = new InputFilter() {
@Override
public CharSequence filter(
CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// Accept deletions.
if (start == end) return null;
// Accept digits, "-", and spaces.
if (cardNumberPattern.matcher(source.subSequence(start, end)).matches()) {
return null;
}
// Reject everything else.
return "";
}
};
mCardNumberFormatter = new CreditCardNumberFormattingTextWatcher();
mPhoneFormatter = new PhoneNumberUtil.FormatTextWatcher();
}
public static Void onEditorAction(TextView.OnEditorActionListener arg) {
return BaseDSL.attr("onEditorAction", arg);
}
public static Void onEditorAction(TextView.OnEditorActionListener arg) {
return BaseDSL.attr("onEditorAction", arg);
}
public void setOnEditorActionListener(TextView.OnEditorActionListener actionListener) {
binding.editSearch.setOnEditorActionListener(actionListener);
}
public void setOnEditorActionListener(TextView.OnEditorActionListener onEditorActionListener){
mOnEditorActionListener = onEditorActionListener;
}