自定义编辑文本可以正常工作,但某些ViVO设备不起作用

时间:2019-01-09 09:05:42

标签: android android-edittext

我为添加标签创建了一个自定义编辑文本视图,该标签对我来说可以正常工作,但是某些vivo设备对它不起作用。

场景是,当我键入像char A这样的char时,字符序列将为'A',之后我键入B则字符序列将为'AB',然后我键入C char序列应为'ABC',但其不接受char C并也删除“ B”。

 public class TagEditText extends android.support.v7.widget.AppCompatEditText {

TextWatcher textWatcher;
String lastString;
String separator = " ";
CharSequence sts = "";

public TagEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}


private void init() {
    setMovementMethod(LinkMovementMethod.getInstance());

    textWatcher = 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) {
            Log.i("CharSequence1", s.toString());
            sts = s;
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.i("CharSequence2", s.toString());
            String thisString = s.toString();
            if (thisString.length() > 0 && !thisString.equals(lastString)) {
                format();
            }
        }
    };

    addTextChangedListener(textWatcher);
}


private void format() {


    SpannableStringBuilder sb = new SpannableStringBuilder();
    String fullString = getText().toString();

    String[] strings = fullString.split(separator);
    for (int i = 0; i < strings.length; i++) {

        String string = strings[i];
        sb.append(string);
        if (fullString.charAt(fullString.length() - 1) != separator.charAt(0) && i == strings.length - 1) {
            break;
        }
        BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(createTokenView(string));
        bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());

        int startIdx = sb.length() - (string.length());
        int endIdx = sb.length();

        sb.setSpan(new ImageSpan(bd), startIdx, endIdx, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        MyClickableSpan myClickableSpan = new MyClickableSpan(startIdx, endIdx);
        sb.setSpan(myClickableSpan, Math.max(endIdx - 2, startIdx), endIdx, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        if (i < strings.length - 1) {
            sb.append(separator);
        } else if (fullString.charAt(fullString.length() - 1) == separator.charAt(0)) {
            sb.append(separator);
        }
    }

    lastString = sb.toString();

    setText(sb);
    setSelection(sb.length());

}

public View createTokenView(String text) {

    LinearLayout l = new LinearLayout(getContext());
    l.setOrientation(LinearLayout.HORIZONTAL);
    l.setGravity(Gravity.CENTER_VERTICAL);
    l.setBackgroundResource(R.drawable.bordered_rectangle_rounded_corners);

    TextView tv = new TextView(getContext());
    l.addView(tv);
    tv.setText(text);
    tv.setPadding(0, 0, 5, 0);
    tv.setTextColor(ContextCompat.getColor(getContext(), R.color.chocolateTextColor));
    tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);

    ImageView im = new ImageView(getContext());
    l.addView(im);
    im.setImageResource(R.drawable.delete_tag);
    im.setScaleType(ImageView.ScaleType.FIT_CENTER);

    return l;
}

public Object convertViewToDrawable(View view) {
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(spec, spec);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(c);
    view.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = view.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    view.destroyDrawingCache();
    return new BitmapDrawable(getContext().getResources(), viewBmp);
}

private class MyClickableSpan extends ClickableSpan {

    int startIdx;
    int endIdx;

    public MyClickableSpan(int startIdx, int endIdx) {
        super();
        this.startIdx = startIdx;
        this.endIdx = endIdx;
    }

    @Override
    public void onClick(View widget) {
        String s = getText().toString();
        String s1 = s.substring(0, startIdx);
        String s2 = s.substring(Math.min(endIdx + 1, s.length() - 1), s.length());
        TagEditText.this.setText(s1 + s2);
    }

}
}


XML code

<com.thevipgroup.views.TagEditText
        android:id="@+id/tags"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/dp_30"
        android:layout_marginStart="@dimen/dp_30"
        android:layout_marginTop="@dimen/dp_20"
        android:background="@drawable/white_round_semi_transparent"
        android:cursorVisible="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center_vertical"
        android:hint="@string/tags_hint"
        android:maxHeight="@dimen/dp_150"
        android:minHeight="@dimen/dp_45"
        android:paddingLeft="@dimen/dp_16"
        android:paddingRight="@dimen/dp_16"
        android:singleLine="true"
        android:textColor="@color/chocolateTextColor"
        android:textColorHint="@color/textHintColorGrey"
        android:textSize="@dimen/sp_15"
        fontpath:customFont="fonts/SF-UI-DISPLAY-REGULAR.TTF" />

0 个答案:

没有答案