下面列出了android.text.method.DialerKeyListener#android.text.method.TextKeyListener 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
final int uniChar = event.getUnicodeChar();
final boolean handled = super.onKeyDown(keyCode, event);
final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
if (!handled && acceptFilter() && isKeyNotWhitespace) {
boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
keyCode, event);
if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
// something usable has been typed - start a search
// the typed text will be retrieved and cleared by
// showSearchDialog()
// If there are multiple keystrokes before the search dialog takes focus,
// onSearchRequested() will be called for every keystroke,
// but it is idempotent, so it's fine.
return onSearchRequested();
}
}
// Eat the long press event so the keyboard doesn't come up.
if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
return true;
}
return handled;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// Determine if the key event was actual text, if so, focus the search bar and then dispatch
// the key normally so that it can process this key event
if (!mSearchBarController.isSearchFieldFocused() &&
event.getAction() == KeyEvent.ACTION_DOWN) {
final int unicodeChar = event.getUnicodeChar();
final boolean isKeyNotWhitespace = unicodeChar > 0 &&
!Character.isWhitespace(unicodeChar) && !Character.isSpaceChar(unicodeChar);
if (isKeyNotWhitespace) {
boolean gotKey = TextKeyListener.getInstance().onKeyDown(this, mSearchQueryBuilder,
event.getKeyCode(), event);
if (gotKey && mSearchQueryBuilder.length() > 0) {
mSearchBarController.focusSearchField();
}
}
}
return super.dispatchKeyEvent(event);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// Determine if the key event was actual text, if so, focus the search bar and then dispatch
// the key normally so that it can process this key event
if (mSearchBarController != null &&
!mSearchBarController.isSearchFieldFocused() &&
event.getAction() == KeyEvent.ACTION_DOWN) {
final int unicodeChar = event.getUnicodeChar();
final boolean isKeyNotWhitespace = unicodeChar > 0 &&
!Character.isWhitespace(unicodeChar) && !Character.isSpaceChar(unicodeChar);
if (isKeyNotWhitespace) {
boolean gotKey = TextKeyListener.getInstance().onKeyDown(this, mSearchQueryBuilder,
event.getKeyCode(), event);
if (gotKey && mSearchQueryBuilder.length() > 0) {
mSearchBarController.focusSearchField();
}
}
}
return super.dispatchKeyEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
final int uniChar = event.getUnicodeChar();
final boolean handled = super.onKeyDown(keyCode, event);
final boolean isKeyNotWhitespace = uniChar > 0
&& !Character.isWhitespace(uniChar);
if (!handled && acceptFilter() && isKeyNotWhitespace) {
boolean gotKey = TextKeyListener.getInstance().onKeyDown(
mWorkspace, mDefaultKeySsb, keyCode, event);
if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
return onSearchRequested();
}
}
if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
return true;
}
return handled;
}
@Override
public boolean onKeyDown(View view, Editable content, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
TextKeyListener.clear(content);
mHidPayload.assemblePayload(HidKeyPair.ENTER);
mSocketManager.sendPayload(mHidPayload);
return true;
case KeyEvent.KEYCODE_DEL:
mHidPayload.assemblePayload(HidKeyPair.DEL);
mSocketManager.sendPayload(mHidPayload);
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
mHidPayload.assemblePayload(keyCode);
mSocketManager.sendPayload(mHidPayload);
default:
return mTextKeyListener.onKeyDown(view, content, keyCode, event);
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// Setup the filter view
mInputFilters = new InputFilter[] { new InputFilter.AllCaps() };
mHint = (EditText) findViewById(com.android.internal.R.id.hint);
if (mHint == null) {
throw new IllegalStateException("DialerFilter must have a child EditText named hint");
}
mHint.setFilters(mInputFilters);
mLetters = mHint;
mLetters.setKeyListener(TextKeyListener.getInstance());
mLetters.setMovementMethod(null);
mLetters.setFocusable(false);
// Setup the digits view
mPrimary = (EditText) findViewById(com.android.internal.R.id.primary);
if (mPrimary == null) {
throw new IllegalStateException("DialerFilter must have a child EditText named primary");
}
mPrimary.setFilters(mInputFilters);
mDigits = mPrimary;
mDigits.setKeyListener(DialerKeyListener.getInstance());
mDigits.setMovementMethod(null);
mDigits.setFocusable(false);
// Look for an icon
mIcon = (ImageView) findViewById(com.android.internal.R.id.icon);
// Setup focus & highlight for this view
setFocusable(true);
// XXX Force the mode to QWERTY for now, since 12-key isn't supported
mIsQwerty = true;
setMode(DIGITS_AND_LETTERS);
}
public static TextKeyListener getInstance(boolean autotext, Capitalize cap) {
int off = cap.ordinal() * 2 + (autotext ? 1 : 0);
if (sInstance[off] == null) {
sInstance[off] = new JotaTextKeyListener(cap, autotext);
}
return sInstance[off];
}
@Override
public void onDestroy() {
super.onDestroy();
mWorkspace.removeCallbacks(mBuildLayersRunnable);
mWorkspace.removeFolderListeners();
// Stop callbacks from LauncherModel
// It's possible to receive onDestroy after a new Launcher activity has
// been created. In this case, don't interfere with the new Launcher.
if (mModel.isCurrentCallbacks(this)) {
mModel.stopLoader();
LauncherAppState.getInstance(this).setLauncher(null);
}
if (mRotationPrefChangeHandler != null) {
mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
}
try {
mAppWidgetHost.stopListening();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
mAppWidgetHost = null;
TextKeyListener.getInstance().release();
((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
.removeAccessibilityStateChangeListener(this);
LauncherAnimUtils.onDestroyActivity();
if (mLauncherCallbacks != null) {
mLauncherCallbacks.onDestroy();
}
}
public static TextKeyListener getInstance(boolean autotext, Capitalize cap) {
int off = cap.ordinal() * 2 + (autotext ? 1 : 0);
if (sInstance[off] == null) {
sInstance[off] = new JotaTextKeyListener(cap, autotext);
}
return sInstance[off];
}
public void setupAutoComplete () {
if (this.txtBody.getAdapter() != null) return;
this.txtBody.setThreshold(1);
this.txtBody.setTokenizer(new UsernameTokenizer());
this.txtBody.setAdapter(new UsernameSearchAdapter(this));
this.txtBody.addTextChangedListener(new PopupPositioniner(this.txtBody));
this.txtBody.setKeyListener(TextKeyListener.getInstance(true, TextKeyListener.Capitalize.SENTENCES));
}
/**
* Fills in the specified Path with a representation of a cursor
* at the specified offset. This will often be a vertical line
* but can be multiple discontinuous lines in text with multiple
* directionalities.
*/
public void getCursorPath(final int point, final Path dest, final CharSequence editingBuffer) {
dest.reset();
int line = getLineForOffset(point);
int top = getLineTop(line);
int bottom = getLineBottomWithoutSpacing(line);
boolean clamped = shouldClampCursor(line);
float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
int dist = 0;
if (caps != 0 || fn != 0) {
dist = (bottom - top) >> 2;
if (fn != 0)
top += dist;
if (caps != 0)
bottom -= dist;
}
if (h1 < 0.5f)
h1 = 0.5f;
if (h2 < 0.5f)
h2 = 0.5f;
if (Float.compare(h1, h2) == 0) {
dest.moveTo(h1, top);
dest.lineTo(h1, bottom);
} else {
dest.moveTo(h1, top);
dest.lineTo(h1, (top + bottom) >> 1);
dest.moveTo(h2, (top + bottom) >> 1);
dest.lineTo(h2, bottom);
}
if (caps == 2) {
dest.moveTo(h2, bottom);
dest.lineTo(h2 - dist, bottom + dist);
dest.lineTo(h2, bottom);
dest.lineTo(h2 + dist, bottom + dist);
} else if (caps == 1) {
dest.moveTo(h2, bottom);
dest.lineTo(h2 - dist, bottom + dist);
dest.moveTo(h2 - dist, bottom + dist - 0.5f);
dest.lineTo(h2 + dist, bottom + dist - 0.5f);
dest.moveTo(h2 + dist, bottom + dist);
dest.lineTo(h2, bottom);
}
if (fn == 2) {
dest.moveTo(h1, top);
dest.lineTo(h1 - dist, top - dist);
dest.lineTo(h1, top);
dest.lineTo(h1 + dist, top - dist);
} else if (fn == 1) {
dest.moveTo(h1, top);
dest.lineTo(h1 - dist, top - dist);
dest.moveTo(h1 - dist, top - dist + 0.5f);
dest.lineTo(h1 + dist, top - dist + 0.5f);
dest.moveTo(h1 + dist, top - dist);
dest.lineTo(h1, top);
}
}
/**
* Fills in the specified Path with a representation of a cursor
* at the specified offset. This will often be a vertical line
* but can be multiple discontinous lines in text with multiple
* directionalities.
*/
public void getCursorPath(int point, Path dest,
CharSequence editingBuffer) {
dest.reset();
int line = getLineForOffset(point);
int top = getLineTop(line);
int bottom = getLineTop(line+1);
float h1 = getPrimaryHorizontal(point) - 0.5f;
float h2 = getSecondaryHorizontal(point) - 0.5f;
int caps = TextKeyListener.getMetaState(editingBuffer,
KeyEvent.META_SHIFT_ON) |
JotaTextKeyListener.getMetaStateSelecting(editingBuffer);
int fn = TextKeyListener.getMetaState(editingBuffer,
KeyEvent.META_ALT_ON);
int dist = 0;
if (caps != 0 || fn != 0) {
dist = (bottom - top) >> 2;
if (fn != 0)
top += dist;
if (caps != 0)
bottom -= dist;
}
if (h1 < 0.5f)
h1 = 0.5f;
if (h2 < 0.5f)
h2 = 0.5f;
if (h1 == h2) {
dest.moveTo(h1, top);
dest.lineTo(h1, bottom);
} else {
dest.moveTo(h1, top);
dest.lineTo(h1, (top + bottom) >> 1);
dest.moveTo(h2, (top + bottom) >> 1);
dest.lineTo(h2, bottom);
}
if (caps == 2) {
dest.moveTo(h2, bottom);
dest.lineTo(h2 - dist, bottom + dist);
dest.lineTo(h2, bottom);
dest.lineTo(h2 + dist, bottom + dist);
} else if (caps == 1) {
dest.moveTo(h2, bottom);
dest.lineTo(h2 - dist, bottom + dist);
dest.moveTo(h2 - dist, bottom + dist - 0.5f);
dest.lineTo(h2 + dist, bottom + dist - 0.5f);
dest.moveTo(h2 + dist, bottom + dist);
dest.lineTo(h2, bottom);
}
if (fn == 2) {
dest.moveTo(h1, top);
dest.lineTo(h1 - dist, top - dist);
dest.lineTo(h1, top);
dest.lineTo(h1 + dist, top - dist);
} else if (fn == 1) {
dest.moveTo(h1, top);
dest.lineTo(h1 - dist, top - dist);
dest.moveTo(h1 - dist, top - dist + 0.5f);
dest.lineTo(h1 + dist, top - dist + 0.5f);
dest.moveTo(h1 + dist, top - dist);
dest.lineTo(h1, top);
}
}
@Override
public void onDestroy() {
super.onDestroy();
// Remove all pending runnables
mHandler.removeMessages(ADVANCE_MSG);
mHandler.removeMessages(0);
mWorkspace.removeCallbacks(mBuildLayersRunnable);
// Stop callbacks from LauncherModel
LauncherAppState app = (LauncherAppState.getInstance());
mModel.stopLoader();
app.setLauncher(null);
try {
mAppWidgetHost.stopListening();
} catch (NullPointerException ex) {
Log.w(TAG,
"problem while stopping AppWidgetHost during Launcher destruction",
ex);
}
mAppWidgetHost = null;
mWidgetsToAdvance.clear();
TextKeyListener.getInstance().release();
// Disconnect any of the callbacks and drawables associated with
// ItemInfos on the workspace
// to prevent leaking Launcher activities on orientation change.
if (mModel != null) {
mModel.unbindItemInfosAndClearQueuedBindRunnables();
}
getContentResolver().unregisterContentObserver(mWidgetObserver);
unregisterReceiver(mCloseSystemDialogsReceiver);
mDragLayer.clearAllResizeFrames();
((ViewGroup) mWorkspace.getParent()).removeAllViews();
mWorkspace.removeAllWorkspaceScreens();
mWorkspace = null;
mDragController = null;
LauncherAnimUtils.onDestroyActivity();
unregisterReceiver(protectedAppsChangedReceiver);
}
/**
* Fills in the specified Path with a representation of a cursor
* at the specified offset. This will often be a vertical line
* but can be multiple discontinous lines in text with multiple
* directionalities.
*/
public void getCursorPath(int point, Path dest,
CharSequence editingBuffer) {
dest.reset();
int line = getLineForOffset(point);
int top = getLineTop(line);
int bottom = getLineTop(line+1);
float h1 = getPrimaryHorizontal(point) - 0.5f;
float h2 = getSecondaryHorizontal(point) - 0.5f;
int caps = TextKeyListener.getMetaState(editingBuffer,
KeyEvent.META_SHIFT_ON) |
JotaTextKeyListener.getMetaStateSelecting(editingBuffer);
int fn = TextKeyListener.getMetaState(editingBuffer,
KeyEvent.META_ALT_ON);
int dist = 0;
if (caps != 0 || fn != 0) {
dist = (bottom - top) >> 2;
if (fn != 0)
top += dist;
if (caps != 0)
bottom -= dist;
}
if (h1 < 0.5f)
h1 = 0.5f;
if (h2 < 0.5f)
h2 = 0.5f;
if (h1 == h2) {
dest.moveTo(h1, top);
dest.lineTo(h1, bottom);
} else {
dest.moveTo(h1, top);
dest.lineTo(h1, (top + bottom) >> 1);
dest.moveTo(h2, (top + bottom) >> 1);
dest.lineTo(h2, bottom);
}
if (caps == 2) {
dest.moveTo(h2, bottom);
dest.lineTo(h2 - dist, bottom + dist);
dest.lineTo(h2, bottom);
dest.lineTo(h2 + dist, bottom + dist);
} else if (caps == 1) {
dest.moveTo(h2, bottom);
dest.lineTo(h2 - dist, bottom + dist);
dest.moveTo(h2 - dist, bottom + dist - 0.5f);
dest.lineTo(h2 + dist, bottom + dist - 0.5f);
dest.moveTo(h2 + dist, bottom + dist);
dest.lineTo(h2, bottom);
}
if (fn == 2) {
dest.moveTo(h1, top);
dest.lineTo(h1 - dist, top - dist);
dest.lineTo(h1, top);
dest.lineTo(h1 + dist, top - dist);
} else if (fn == 1) {
dest.moveTo(h1, top);
dest.lineTo(h1 - dist, top - dist);
dest.moveTo(h1 - dist, top - dist + 0.5f);
dest.lineTo(h1 + dist, top - dist + 0.5f);
dest.moveTo(h1 + dist, top - dist);
dest.lineTo(h1, top);
}
}
public StringWidget(Context context, FormEntryPrompt prompt, boolean secret, boolean inCompactGroup) {
super(context, prompt, inCompactGroup);
mAnswer = (EditText)LayoutInflater.from(getContext()).inflate(getAnswerLayout(), this, false);
mAnswer.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontSize);
mAnswer.setOnClickListener(this);
mAnswer.addTextChangedListener(this);
//Let's see if we can figure out a constraint for this string
try {
addAnswerFilter(new InputFilter.LengthFilter(guessMaxStringLength(prompt)));
} catch (UnpivotableExpressionException e) {
//expected if there isn't a constraint that does this
}
this.secret = secret;
if (!secret) {
// capitalize the first letter of the sentence
mAnswer.setKeyListener(new TextKeyListener(Capitalize.SENTENCES, false));
}
setTextInputType(mAnswer);
if (!secret) {
mAnswer.setSingleLine(false);
}
if (prompt != null) {
mReadOnly = prompt.isReadOnly();
IAnswerData value = prompt.getAnswerValue();
if (value != null) {
mAnswer.setText(value.getDisplayText());
}
if (mReadOnly) {
if (value == null) {
mAnswer.setText("---");
}
mAnswer.setBackgroundDrawable(null);
mAnswer.setFocusable(false);
mAnswer.setClickable(false);
}
}
if (isInCompactMode()) {
addToCompactLayout(mAnswer);
} else {
addView(mAnswer);
}
}
public KeyboardKeyListener(SocketManager socketManager) {
super();
mTextKeyListener = new TextKeyListener(Capitalize.NONE, false);
mSocketManager = socketManager;
}