下面列出了android.view.textservice.SuggestionsInfo#RESULT_ATTR_LOOKS_LIKE_TYPO 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Checks for typos and sends results back to native through a JNI call.
* @param results Results returned by the Android spellchecker.
*/
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
ArrayList<Integer> offsets = new ArrayList<Integer>();
ArrayList<Integer> lengths = new ArrayList<Integer>();
for (SentenceSuggestionsInfo result : results) {
for (int i = 0; i < result.getSuggestionsCount(); i++) {
// If a word looks like a typo, record its offset and length.
if ((result.getSuggestionsInfoAt(i).getSuggestionsAttributes()
& SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO)
== SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) {
offsets.add(result.getOffsetAt(i));
lengths.add(result.getLengthAt(i));
}
}
}
nativeProcessSpellCheckResults(mNativeSpellCheckerSessionBridge,
convertListToArray(offsets), convertListToArray(lengths));
}
/**
* Checks for typos and sends results back to native through a JNI call.
* @param results Results returned by the Android spellchecker.
*/
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
mStopMs = SystemClock.elapsedRealtime();
if (mNativeSpellCheckerSessionBridge == 0) {
return;
}
ArrayList<Integer> offsets = new ArrayList<Integer>();
ArrayList<Integer> lengths = new ArrayList<Integer>();
for (SentenceSuggestionsInfo result : results) {
if (result == null) {
// In some cases null can be returned by the selected spellchecking service,
// see crbug.com/651458. In this case skip to next result to avoid a
// NullPointerException later on.
continue;
}
for (int i = 0; i < result.getSuggestionsCount(); i++) {
// If a word looks like a typo, record its offset and length.
if ((result.getSuggestionsInfoAt(i).getSuggestionsAttributes()
& SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO)
== SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) {
offsets.add(result.getOffsetAt(i));
lengths.add(result.getLengthAt(i));
}
}
}
nativeProcessSpellCheckResults(mNativeSpellCheckerSessionBridge,
convertListToArray(offsets), convertListToArray(lengths));
RecordHistogram.recordTimesHistogram("SpellCheck.Android.Latency",
mStopMs - mStartMs, TimeUnit.MILLISECONDS);
}
private SpellCheckSpan onGetSuggestionsInternal(
SuggestionsInfo suggestionsInfo, int offset, int length) {
if (suggestionsInfo == null || suggestionsInfo.getCookie() != mCookie) {
return null;
}
final Editable editable = (Editable) mTextView.getText();
final int sequenceNumber = suggestionsInfo.getSequence();
for (int k = 0; k < mLength; ++k) {
if (sequenceNumber == mIds[k]) {
final int attributes = suggestionsInfo.getSuggestionsAttributes();
final boolean isInDictionary =
((attributes & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) > 0);
final boolean looksLikeTypo =
((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0);
final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[k];
//TODO: we need to change that rule for results from a sentence-level spell
// checker that will probably be in dictionary.
if (!isInDictionary && looksLikeTypo) {
createMisspelledSuggestionSpan(
editable, suggestionsInfo, spellCheckSpan, offset, length);
} else {
// Valid word -- isInDictionary || !looksLikeTypo
if (mIsSentenceSpellCheckSupported) {
// Allow the spell checker to remove existing misspelled span by
// overwriting the span over the same place
final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
final int start;
final int end;
if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
start = spellCheckSpanStart + offset;
end = start + length;
} else {
start = spellCheckSpanStart;
end = spellCheckSpanEnd;
}
if (spellCheckSpanStart >= 0 && spellCheckSpanEnd > spellCheckSpanStart
&& end > start) {
final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
if (tempSuggestionSpan != null) {
if (DBG) {
Log.i(TAG, "Remove existing misspelled span. "
+ editable.subSequence(start, end));
}
editable.removeSpan(tempSuggestionSpan);
mSuggestionSpanCache.remove(key);
}
}
}
}
return spellCheckSpan;
}
}
return null;
}
/**
* Returns an empty SuggestionsInfo with flags signaling the word is not in the dictionary.
* @param reportAsTypo whether this should include the flag LOOKS_LIKE_TYPO, for red underline.
* @return the empty SuggestionsInfo with the appropriate flags set.
*/
public static SuggestionsInfo getNotInDictEmptySuggestions(final boolean reportAsTypo) {
return new SuggestionsInfo(reportAsTypo ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0,
EMPTY_STRING_ARRAY);
}
/**
* Returns an empty SuggestionsInfo with flags signaling the word is not in the dictionary.
* @param reportAsTypo whether this should include the flag LOOKS_LIKE_TYPO, for red underline.
* @return the empty SuggestionsInfo with the appropriate flags set.
*/
public static SuggestionsInfo getNotInDictEmptySuggestions(final boolean reportAsTypo) {
return new SuggestionsInfo(reportAsTypo ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0,
EMPTY_STRING_ARRAY);
}
/**
* Returns an empty SuggestionsInfo with flags signaling the word is not in the dictionary.
* @param reportAsTypo whether this should include the flag LOOKS_LIKE_TYPO, for red underline.
* @return the empty SuggestionsInfo with the appropriate flags set.
*/
public static SuggestionsInfo getNotInDictEmptySuggestions(final boolean reportAsTypo) {
return new SuggestionsInfo(reportAsTypo ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0,
EMPTY_STRING_ARRAY);
}
/**
* Returns an empty SuggestionsInfo with flags signaling the word is not in the dictionary.
* @param reportAsTypo whether this should include the flag LOOKS_LIKE_TYPO, for red underline.
* @return the empty SuggestionsInfo with the appropriate flags set.
*/
public static SuggestionsInfo getNotInDictEmptySuggestions(final boolean reportAsTypo) {
return new SuggestionsInfo(reportAsTypo ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0,
EMPTY_STRING_ARRAY);
}