下面列出了android.text.Layout#getLineMax ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Calculate the right boundary for this run (harder than it sounds). As we're a letter ahead,
* need to grab either current letter start or the end of the previous line. Also need to
* consider maxLines case, which inserts ellipses at the overflow point – don't include these.
*/
private int getRunRight(
Layout unrestrictedLayout, Layout maxLinesLayout, int currentLine, int index,
int line, boolean withinMax, boolean isMaxEllipsis, boolean isLastChar) {
int runRight;
if (line != currentLine || isLastChar) {
if (isMaxEllipsis) {
runRight = (int) maxLinesLayout.getPrimaryHorizontal(index);
} else {
runRight = (int) unrestrictedLayout.getLineMax(currentLine);
}
} else {
if (withinMax) {
runRight = (int) maxLinesLayout.getPrimaryHorizontal(index);
} else {
runRight = (int) unrestrictedLayout.getPrimaryHorizontal(index);
}
}
return runRight;
}
private void openSearchInternal(Boolean openKeyboard) {
this.mLogoView.setVisibility(View.GONE);
this.mSearchEditText.setVisibility(View.VISIBLE);
mSearchEditText.requestFocus();
this.mSuggestionListView.setVisibility(View.VISIBLE);
mSuggestionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
hideKeyboard();
NiboSearchSuggestionItem result = mSearchSuggestions.get(arg2);
if (mSearchListener != null) {
if (mSearchListener.onSuggestion(result)) {
setSearchString(result.getValue(), true);
fromEditingToSearch(true, false);
}
} else {
setSearchString(result.getValue(), true);
fromEditingToSearch(true, false);
}
}
});
String currentSearchText = getSearchText();
if (currentSearchText.length() > 0) {
buildSearchSuggestions(currentSearchText);
} else {
buildEmptySearchSuggestions();
}
if (mSearchListener != null)
mSearchListener.onSearchEditOpened();
if (getSearchText().length() > 0) {
showClearButton();
}
if (openKeyboard) {
if (showCustomKeyboard && mCustomKeyboardView != null) { // Show custom keyboard
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.setEnabled(true);
// Enable cursor, but still prevent default keyboard from showing up
OnTouchListener otl = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.setEnabled(true);
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + mSearchEditText.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if (offset > 0)
if (x > layout.getLineMax(0))
mSearchEditText.setSelection(offset); // Touch was at the end of the text
else
mSearchEditText.setSelection(offset - 1);
break;
case MotionEvent.ACTION_MOVE:
layout = ((EditText) v).getLayout();
x = event.getX() + mSearchEditText.getScrollX();
offset = layout.getOffsetForHorizontal(0, x);
if (offset > 0)
if (x > layout.getLineMax(0))
mSearchEditText.setSelection(offset); // Touch point was at the end of the text
else
mSearchEditText.setSelection(offset - 1);
break;
}
return true;
}
};
mSearchEditText.setOnTouchListener(otl);
} else { // Show default keyboard
mSearchEditText.setOnTouchListener(null);
InputMethodManager inputMethodManager = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
}
}
private void openSearchInternal(Boolean openKeyboard) {
this.mLogoView.setVisibility(View.GONE);
this.mSearchEditText.setVisibility(View.VISIBLE);
mSearchEditText.requestFocus();
this.mSuggestionListView.setVisibility(View.VISIBLE);
mSuggestionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
hideKeyboard();
SearchItem result = mSearchSuggestions.get(arg2);
if (mSearchListener != null) {
if (mSearchListener.onSuggestion(result)) {
setSearchString(result.getValue(), true);
fromEditingToSearch(true, false);
}
} else {
setSearchString(result.getValue(), true);
fromEditingToSearch(true, false);
}
}
});
String currentSearchText = getSearchText();
if (currentSearchText.length() > 0) {
buildSearchSuggestions(currentSearchText);
} else {
buildEmptySearchSuggestions();
}
if (mSearchListener != null)
mSearchListener.onSearchEditOpened();
if (getSearchText().length() > 0) {
showClearButton();
}
if (openKeyboard) {
if(showCustomKeyboard && mCustomKeyboardView != null) { // Show custom keyboard
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.setEnabled(true);
// Enable cursor, but still prevent default keyboard from showing up
OnTouchListener otl = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.setEnabled(true);
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + mSearchEditText.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if (offset > 0)
if (x > layout.getLineMax(0))
mSearchEditText.setSelection(offset); // Touch was at the end of the text
else
mSearchEditText.setSelection(offset - 1);
break;
case MotionEvent.ACTION_MOVE:
layout = ((EditText) v).getLayout();
x = event.getX() + mSearchEditText.getScrollX();
offset = layout.getOffsetForHorizontal(0, x);
if (offset > 0)
if (x > layout.getLineMax(0))
mSearchEditText.setSelection(offset); // Touch point was at the end of the text
else
mSearchEditText.setSelection(offset - 1);
break;
}
return true;
}
};
mSearchEditText.setOnTouchListener(otl);
} else { // Show default keyboard
mSearchEditText.setOnTouchListener(null);
InputMethodManager inputMethodManager = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
}
}