gpt4 book ai didi

java - 编辑文本删除退格键上的跨度。显示纯文本

转载 作者:行者123 更新时间:2023-12-05 07:51:26 24 4
gpt4 key购买 nike

我正在使用编辑文本来显示标签类型的 View 。使用 SpannableStringBuilder 类来设置跨度。但是,在按下退格键时,应该删除跨度以显示纯文本。如何实现。

代码如下:

hashTags.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count >= 1 && !isEdit) {
if (!Character.isSpaceChar(s.charAt(0))) {
if (s.charAt(start) == ' ')
setTag(); // generate chips
} else {
hashTags.getText().clear();
hashTags.setSelection(0);
}

}

}

@Override
public void afterTextChanged(Editable s) {
if (isEdit) {
setTag();
}

}
});
public void setTag() {
if (hashTags.getText().toString().contains(" "))
{

SpannableStringBuilder ssb = new SpannableStringBuilder(hashTags.getText());
String chips[] =hashTags.getText().toString().trim().split(" ");
int x = 0;
tags.clear();
for (String c : chips) {
LayoutInflater lf = (LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
TextView textView = (TextView) lf.inflate(
R.layout.tag_edittext, null);

tags.add(c);

/*textView.setCompoundDrawablesWithIntrinsicBounds(0,
0, android.R.drawable.ic_delete, 0);*/
textView.setText(c); // set text
int spec = View.MeasureSpec.makeMeasureSpec(0,
View.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
BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
int width = bmpDrawable.getIntrinsicWidth() ;
int height = bmpDrawable.getIntrinsicHeight() ;

bmpDrawable.setBounds(0, 0, width, height);
ssb.setSpan(new ImageSpan(bmpDrawable), x, x + c.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

x = x + c.length() + 1;
}

isEdit = false ;
hashTags.setText(ssb);
hashTags.setSelection(hashTags.getText().length());
}

}

最佳答案

检查此文本 Wacher 代码并将其分配给您的 editText。多亏了“if(after==0)”,我们可以计算出 BackSpace 按钮被点击,并且通过为编辑文本分配一个新值,我们可以简单地删除跨度

我在 Kotlin 中的代码:

       val textWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
if(after==0 && s.length>1){

et_sessionTag.setText(et_sessionTag.text.toString().substring(0, s.length - 1))
et_sessionTag.setSelection(et_sessionTag.text.length)

}
}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
//fill if you needed
}


override fun afterTextChanged(s: Editable) {
//fill if you needed
}

关于java - 编辑文本删除退格键上的跨度。显示纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com