【发布时间】:2013-08-09 20:11:10
【问题描述】:
我正在创建一个具有“收件人”字段的应用程序,就像 Facebook 应用程序的“新消息”功能一样。
从下拉列表中选择一个项目后,我创建一个图像跨度并将其添加到MultiAutoCompleteTextView。我在这个视图中使用了SpaceTokenizer。问题是当我点击退格时,光标首先移动到空白处(即空格Tokenizer),然后当我再次点击退格时,整个单词被删除......我想删除整个我第一次点击退格键就像 facebook 应用程序一样......
这是我的SpaceTokenizer代码
multiContentText.setTokenizer(new Tokenizer(){
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
if(i>0){
Log.d("textchar ",""+text.charAt(i - 1));
}
while (i > 0 && text.charAt(i - 1) != ' ') {
i--;
}
while (i < cursor && text.charAt(i) == ' ' || text.charAt(i - 1) == '\n') {
i++;
}
return i;
}
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == ' ' || text.charAt(i - 1) == '\n') {
return i;
} else {
i++;
}
}
return len;
}
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n') {
i--;
}
if (i > 0 && text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n') {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text+" ";
}
}
}
});
我正在使用此代码在我的多 ContentText 中创建 TextView
SpannableStringBuilder ssb = new SpannableStringBuilder(multiContentText.getText());
String c="text from the list";
TextView textView = (TextView) inflater.inflate(R.layout.chips_edittext, null);
textView.setText(c); // set text
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(spec, spec);
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.translate(-textView.getScrollX(), -textView.getScrollY());
textView.draw(canvas);
textView.setDrawingCacheEnabled(true);
Bitmap cacheBmp = textView.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
textView.destroyDrawingCache(); // destory drawable
// create bitmap drawable for imagespan
BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
bmpDrawable.setBounds(0, 0,bmpDrawable.getIntrinsicWidth(),bmpDrawable.getIntrinsicHeight());
// create and set imagespan
ssb.setSpan(new ImageSpan(bmpDrawable),0 ,c.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// set chips span
multiContentText.setText(ssb);
multiContentText.setSelection(multiContentText.getText().length());
我不确定空间 Tokenizer 是否适合这种行为...任何帮助或指点将不胜感激...
这里是截图以便更好地理解......
我有一个文本,后跟一个空格,然后是一个光标...如果我按退格键,它首先会移动到空白处,只有当我再次按退格键时,整个文本才会被删除....
这是另一个截图..
这里的光标并不完全在两个 TextViews 之间,这与 facebook 应用程序不同,这再次导致插入文本时出现一些问题...
【问题讨论】:
-
您是否尝试在获取长度之前修剪字符序列文本??
-
是的..我尝试修剪长度..当我这样做时,光标会立即出现在 textview 之后,这会导致建议列表消失,因为之前没有空格..我已经添加了截图这个问题,请看一下以便更好地理解。
-
您好,您解决了这个问题吗?如果您有,请您发布示例代码.. @VijayRaj
-
如果你想使用 goole 的代码,看看这个:plus.google.com/+RichHyndman/posts/TSxaARVsRjF,这里的代码:android.googlesource.com/platform/frameworks/ex/+/…
-
@VijayRaj 您好,您的回答解决了您的问题吗?因为它对我不起作用..请给我一些建议
标签: android android-layout android-edittext multiautocompletetextview