Java 编辑文本在退格时删除跨距。显示纯文本

Java 编辑文本在退格时删除跨距。显示纯文本,java,android,android-edittext,spannablestring,Java,Android,Android Edittext,Spannablestring,我使用编辑文本来显示标签类视图。使用SpannableStringBuilder类设置跨距。但是,在按backspace键时,应删除跨距以显示纯文本。如何实现这一目标 代码如下: hashTags.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {

我使用编辑文本来显示标签类视图。使用SpannableStringBuilder类设置跨距。但是,在按backspace键时,应删除跨距以显示纯文本。如何实现这一目标

代码如下:

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)”,我们可以计算出单击的退格按钮,通过将新值分配到编辑文本中,我们可以简单地删除跨距

我在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
        }

你在用什么编程语言?C#?JAVA将它添加到你的标签中,并在问题中提及它。它是android。问题和标签中已经提到。更具体地说,它的Java(after==0)并没有告诉我们在所有情况下都删除了一个字符