下面列出了怎么用android.text.method.KeyListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* gets the key listener by type
*/
protected static KeyListener getKeyListenerForType(NumericType type) {
switch (type) {
case DIALPAD:
return new DialerKeyListener();
case INTEGER:
return new DigitsKeyListener();
case SIGNED:
return new DigitsKeyListener(true, false);
case DECIMAL:
return new DigitsKeyListener(true, true);
case NONE:
default:
return null;
}
}
/**
* {@inheritDoc}
*/
@Override
protected View createView(Context c) {
et = new EditText(c);
et.setBackgroundResource(R.drawable.oval);
et.setTextColor(c.getResources()
.getColorStateList(R.color.primary_text_holo_light));
et.setText(answer);
et.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if (!NumericType.NONE.equals(numericType)) {
KeyListener listener = getKeyListenerForType(numericType);
if (listener != null)
et.setKeyListener(listener);
} else {
et.setRawInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
return encapsulateQuestion(c, et);
}
private void swapPrimaryAndHint(boolean makeLettersPrimary) {
Editable lettersText = mLetters.getText();
Editable digitsText = mDigits.getText();
KeyListener lettersInput = mLetters.getKeyListener();
KeyListener digitsInput = mDigits.getKeyListener();
if (makeLettersPrimary) {
mLetters = mPrimary;
mDigits = mHint;
} else {
mLetters = mHint;
mDigits = mPrimary;
}
mLetters.setKeyListener(lettersInput);
mLetters.setText(lettersText);
lettersText = mLetters.getText();
Selection.setSelection(lettersText, lettersText.length());
mDigits.setKeyListener(digitsInput);
mDigits.setText(digitsText);
digitsText = mDigits.getText();
Selection.setSelection(digitsText, digitsText.length());
// Reset the filters
mPrimary.setFilters(mInputFilters);
mHint.setFilters(mInputFilters);
}
public boolean clearMetaKeyStates(int states) {
final Editable content = getEditable();
if (content == null) return false;
KeyListener kl = mTextView.getKeyListener();
if (kl != null) {
try {
kl.clearMetaKeyState(mTextView, content, states);
} catch (AbstractMethodError e) {
// This is an old listener that doesn't implement the
// new method.
}
}
return true;
}
public boolean clearMetaKeyStates(int states) {
final Editable content = getEditable();
if (content == null) return false;
KeyListener kl = mTextView.getKeyListener();
if (kl != null) {
try {
kl.clearMetaKeyState(mTextView, content, states);
} catch (AbstractMethodError e) {
// This is an old listener that doesn't implement the
// new method.
}
}
return true;
}
/**
* onKeyDown()
*/
@Override
public boolean processKeyDown(int keyCode, KeyEvent event) {
if (mEchoEditText != null && (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
KeyListener keyListener = mEchoEditText.getKeyListener();
keyListener.onKeyDown(mEchoEditText, mEchoEditText.getEditableText(), keyCode, event);
return true;
}
return false;
}
/**
* onKeyUp()
*/
@Override
public boolean processKeyUp(int keyCode, KeyEvent event) {
if (mEchoEditText != null && (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
KeyListener keyListener = mEchoEditText.getKeyListener();
keyListener.onKeyUp(mEchoEditText, mEchoEditText.getEditableText(), keyCode, event);
return true;
}
return false;
}
@Override
public void setKeyListener(KeyListener input) {
super.setKeyListener(input);
}
public static Void keyListener(KeyListener arg) {
return BaseDSL.attr("keyListener", arg);
}
public static Void keyListener(KeyListener arg) {
return BaseDSL.attr("keyListener", arg);
}
/**
* Delegate method for the input widget
*/
public void setInputWidgetKeyListener(KeyListener input) {
getInputWidget().setKeyListener(input);
}
/**
* Delegate method for the input widget
*/
public void setInputWidgetKeyListener(KeyListener input) {
getInputWidget().setKeyListener(input);
}
/**
* 设置 KeyListener
* @param editText {@link EditText}
* @param keyListener {@link KeyListener}
* @param <T> 泛型
* @return {@link EditText}
*/
public static <T extends EditText> T setKeyListener(final T editText, final KeyListener keyListener) {
if (editText != null) {
editText.setKeyListener(keyListener);
}
return editText;
}
/**
* 设置 KeyListener
* @param editText {@link EditText}
* @param keyListener {@link KeyListener}
* @return {@link DevHelper}
*/
public DevHelper setKeyListener(final EditText editText, final KeyListener keyListener) {
EditTextUtils.setKeyListener(editText, keyListener);
return this;
}
/**
* 设置 KeyListener
* @param editText {@link EditText}
* @param keyListener {@link KeyListener}
* @return {@link ViewHelper}
*/
public ViewHelper setKeyListener(final EditText editText, final KeyListener keyListener) {
EditTextUtils.setKeyListener(editText, keyListener);
return this;
}
/**
* @return the current key listener for this TextView.
* This will frequently be null for non-EditText TextViews.
*
* @attr ref android.R.styleable#TextView_numeric
* @attr ref android.R.styleable#TextView_digits
* @attr ref android.R.styleable#TextView_phoneNumber
* @attr ref android.R.styleable#TextView_inputMethod
* @attr ref android.R.styleable#TextView_capitalize
* @attr ref android.R.styleable#TextView_autoText
*/
public final KeyListener getKeyListener (){
return mInputView.getKeyListener();
}
/**
* Sets the key listener to be used with this TextView. This can be null
* to disallow user input. Note that this method has significant and
* subtle interactions with soft keyboards and other input method:
* see {@link KeyListener#getInputType() KeyListener.getContentType()}
* for important details. Calling this method will replace the current
* content type of the text view with the content type returned by the
* key listener.
* <p>
* Be warned that if you want a TextView with a key listener or movement
* method not to be focusable, or if you want a TextView without a
* key listener or movement method to be focusable, you must call
* {@link #setFocusable} again after calling this to get the focusability
* back the way you want it.
*
* @attr ref android.R.styleable#TextView_numeric
* @attr ref android.R.styleable#TextView_digits
* @attr ref android.R.styleable#TextView_phoneNumber
* @attr ref android.R.styleable#TextView_inputMethod
* @attr ref android.R.styleable#TextView_capitalize
* @attr ref android.R.styleable#TextView_autoText
*/
public void setKeyListener (KeyListener input){
mInputView.setKeyListener(input);
}
/**
* @return the current key listener for this TextView. This will frequently be null for
* non-EditText TextViews.
*/
public final KeyListener getKeyListener() {
return getView().getKeyListener();
}
/**
* Sets the key listener to be used with this TextView. This can be null to disallow user input.
* Note that this method has significant and subtle interactions with soft keyboards and other
* input method: see {@link KeyListener#getInputType() KeyListener.getContentType()} for
* important details. Calling this method will replace the current content type of the text view
* with the content type returned by the key listener. <p> Be warned that if you want a TextView
* with a key listener or movement method not to be focusable, or if you want a TextView without
* a key listener or movement method to be focusable, you must call {@link #setFocusable} again
* after calling this to get the focusability back the way you want it.
*/
public final void setKeyListener(final KeyListener input) {
getView().setKeyListener(input);
}
/**
* @return the current key listener for this TextView.
* This will frequently be null for non-EditText TextViews.
*
* @attr ref android.R.styleable#TextView_numeric
* @attr ref android.R.styleable#TextView_digits
* @attr ref android.R.styleable#TextView_phoneNumber
* @attr ref android.R.styleable#TextView_inputMethod
* @attr ref android.R.styleable#TextView_capitalize
* @attr ref android.R.styleable#TextView_autoText
*/
public final KeyListener getKeyListener (){
return mInputView.getKeyListener();
}
/**
* Sets the key listener to be used with this TextView. This can be null
* to disallow user input. Note that this method has significant and
* subtle interactions with soft keyboards and other input method:
* see {@link KeyListener#getInputType() KeyListener.getContentType()}
* for important details. Calling this method will replace the current
* content type of the text view with the content type returned by the
* key listener.
* <p>
* Be warned that if you want a TextView with a key listener or movement
* method not to be focusable, or if you want a TextView without a
* key listener or movement method to be focusable, you must call
* {@link #setFocusable} again after calling this to get the focusability
* back the way you want it.
*
* @attr ref android.R.styleable#TextView_numeric
* @attr ref android.R.styleable#TextView_digits
* @attr ref android.R.styleable#TextView_phoneNumber
* @attr ref android.R.styleable#TextView_inputMethod
* @attr ref android.R.styleable#TextView_capitalize
* @attr ref android.R.styleable#TextView_autoText
*/
public void setKeyListener (KeyListener input){
mInputView.setKeyListener(input);
}