我正在创建一个具有“收件人”字段的应用程序,就像Facebook应用程序的“新消息”功能一样。
从下拉列表中选择一个项目后,我创建了一个imagespan并将其添加到MultiAutoCompleteTextView
。我已将SpaceTokenizer
用于此视图。问题是当我点击退格键时,光标首先移动到空白区域(即空格Tokenizer
),然后当我再次点击退格键时,整个单词被删除....我想要删除我第一次点击退格键就像facebook app ...
以下是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+" ";
}
}
}
});
我正在使用此代码在我的multi-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
是否适合此类行为...任何帮助或指示都会感激......
以下是更好理解的截图....
我有一个文本,后跟一个空格,然后是一个光标...如果我点击退格,它首先会移动到空白区域,只有当我再次点击退格键时,整个文本才被删除....
这是另一个截图..
此处光标不在两个TextView
之间,与facebook应用程序不同,这再次导致插入文本时出现一些问题......
答案 0 :(得分:5)
找到解决方案......
将此textwatcher添加到multiautocompletetextview
private TextWatcher textWather = new TextWatcher() {
int noOfCharAdded=0;int noOfCharDeleted=0;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
startIdx=start;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
noOfCharAdded=after;
noOfCharDeleted=count;
}
@Override
public void afterTextChanged(Editable s) {
Editable buffer = s;
int start = multiContentText.getSelectionStart()<0?0:multiContentText.getSelectionStart();
int end = multiContentText.getSelectionEnd()<0?0:multiContentText.getSelectionEnd();
if(noOfCharAdded==0 && noOfCharDeleted==1){ //if space is deleted
if (start == end && delPrevText) {
ImageSpan link[] = buffer.getSpans(start, end,ImageSpan.class);
if (link.length > 0) {
buffer.replace(buffer.getSpanStart(link[0]),buffer.getSpanEnd(link[0]),"");
buffer.removeSpan(link[0]);
}
}
delPrevText=true;
multiContentText.setSelection(multiContentText.getText().length());
}
else if(noOfCharAdded==0 && noOfCharDeleted>1){//if the whole word is deleted
if(buffer.length()>0){
if(start<buffer.length()){
delPrevText=false;
if(buffer.charAt(start)==' '){
buffer.replace(start,start+1,"");
}
}
}
}
}
};
答案 1 :(得分:2)
尝试向TextWatcher
添加MultiAutoCompleteTextView
。
保存当前文本并检查是否删除了最后一个空格。
如果是,请删除最后一个令牌。
答案 2 :(得分:2)
Editable buffer = s;
int start = multiContentText.getSelectionStart()<0?0:multiContentText.getSelectionStart();
int end = multiContentText.getSelectionEnd()<0?0:multiContentText.getSelectionEnd();
if(noOfCharAdded==0 && noOfCharDeleted==1){ //if space is deleted
if (start == end && delPrevText) {
ImageSpan link[] = buffer.getSpans(start, end,ImageSpan.class);
if (link.length > 0) {
for(int i=0;i<contentArray.size();i++){
JSONObject jo=contentArray.get(i);
try {
int keyValue=jo.getInt("startIndx");//No i18N
if(keyValue==buffer.getSpanStart(link[0])){
jo.put("isRemoved", true);
contentArray.set(i,jo);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
buffer.replace(buffer.getSpanStart(link[0]),buffer.getSpanEnd(link[0]),"");
buffer.removeSpan(link[0]);
}
}
delPrevText=true;
multiContentText.setSelection(multiContentText.getText().length());
}
else if(noOfCharAdded==0 && noOfCharDeleted>1){//if the whole word is deleted
if(buffer.length()>0){
if(start<buffer.length()){
delPrevText=false;
for(int i=0;i<contentArray.size();i++){
JSONObject jo=contentArray.get(i);
try {
int keyValue=jo.getInt("startIndx");//No i18N
if(keyValue==start){
jo.put("isRemoved", true);
contentArray.set(i,jo);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(buffer.charAt(start)==' '){
buffer.replace(start,start+1,"");
}
}
}
}