在我的应用程序中,我通过setSelection(int start,int end)方法以编程方式在EditText上设置开始和结束选择,但是用户没有看到选择标记。如何以编程方式显示标记? 当我说标记时我的意思是:
答案 0 :(得分:3)
我找到了答案。希望它会帮助某人。需要使用方法时需要显示引脚和onSelectionChange需要使用hidePins,因为使用showPins后引脚不会隐藏。
public void showPins() {
Class TV = TextView.class;
try {
Field GetEditor = TV.getDeclaredField("mEditor");
GetEditor.setAccessible(true);
Object editor = GetEditor.get(this);
Method GetInsController = editor.getClass().getDeclaredMethod("getSelectionController");
GetInsController.setAccessible(true);
Object insController = GetInsController.invoke(editor);
if (insController != null) {
Method Show = insController.getClass().getDeclaredMethod("show");
Show.setAccessible(true);
Show.invoke(insController);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void hideTwoPins() {
Class TV = TextView.class;
try {
Field GetEditor = TV.getDeclaredField("mEditor");
GetEditor.setAccessible(true);
Object editor = GetEditor.get(this);
Method GetInsController = editor.getClass().getDeclaredMethod("getSelectionController");
GetInsController.setAccessible(true);
Object insController = GetInsController.invoke(editor);
if (insController != null) {
Method hide = insController.getClass().getDeclaredMethod("hide");
hide.setAccessible(true);
hide.invoke(insController);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
自定义方法
/**
* Method allocates filtering substring in all contacts yellow color,
* that satisfy the user's search
* @param inputText - DisplayName
* filtText - filtering Text
* @return String with allocating substring (Spannable)
*/
public static Spannable changeBackgroungFiltText(CharSequence inputText, String filtText, int color) {
Spannable str = null;
if(inputText != null)
{
String inputStr = inputText.toString();
String inputLowerCaseStr = inputStr.toLowerCase();
String filtLowerCaseStr = filtText.toLowerCase();
// Spannable str = new SpannableStringBuilder(inputStr);
str = new SpannableStringBuilder(inputStr);
if (filtText.length() != 0)
{
int indexStart = 0;
while (true)
{
int indexCur = inputLowerCaseStr.indexOf(filtLowerCaseStr, indexStart);
if (indexCur != -1) {
int start = indexCur;
int end = indexCur + filtText.length();
int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
str.setSpan(new ForegroundColorSpan(color),start, end, flag);
//str.setSpan(new BackgroundColorSpan(highlightColor), start, end, flag);
indexStart = indexCur + 1;
} else {
return str;
}
}
} else {
return str;
}
}
return str;
}
答案 2 :(得分:0)
以下是显示左侧或右侧引脚的方法,
基于第二篇文章的代码(顺便说一句,非常感谢)。
public static void showSelectionPins(TextView textView, boolean left, boolean right) {
Class TV = TextView.class;
try {
Field GetEditor = TV.getDeclaredField("mEditor");
GetEditor.setAccessible(true);
Object editor = GetEditor.get(textView);
Method GetInsController = editor.getClass().getDeclaredMethod("getSelectionController");
GetInsController.setAccessible(true);
Object insController = GetInsController.invoke(editor);
if (insController != null) {
Method Show = insController.getClass().getDeclaredMethod("show");
Show.setAccessible(true);
Show.invoke(insController);
}
/**
* since the {@code mStartHandle} and {@code mEndHandle} are created lazily<br/>
* we must access them after {@code show()} has been called.
*/
if (!left) {
Field SelectionStartHandleView = insController.getClass().getDeclaredField("mStartHandle");
SelectionStartHandleView.setAccessible(true);
Object startHandleView = SelectionStartHandleView.get(insController);
Method handleViewHideMethod = startHandleView.getClass().getSuperclass().getDeclaredMethod("hide");
handleViewHideMethod.setAccessible(true);
handleViewHideMethod.invoke(startHandleView);
}
if (!right) {
Field SelectionEndHandleView = insController.getClass().getDeclaredField("mEndHandle");
SelectionEndHandleView.setAccessible(true);
Object endHandleView = SelectionEndHandleView.get(insController);
Method handleViewHideMethod = endHandleView.getClass().getSuperclass().getDeclaredMethod("hide");
handleViewHideMethod.setAccessible(true);
handleViewHideMethod.invoke(endHandleView);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
以下是显示插入引脚的方法,
也基于第二篇文章的代码(顺便说一句,非常感谢)。
public static void showInsertionPin(TextView textView) {
Class TV = TextView.class;
try {
Field GetEditor = TV.getDeclaredField("mEditor");
GetEditor.setAccessible(true);
Object editor = GetEditor.get(textView);
Method GetInsertionPointCursorController = editor.getClass().getDeclaredMethod("getInsertionController");
GetInsertionPointCursorController.setAccessible(true);
Object insController = GetInsertionPointCursorController.invoke(editor);
if (insController != null) {
Method hide = insController.getClass().getDeclaredMethod("show");
hide.setAccessible(true);
hide.invoke(insController);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}