编辑:我已经能够跟踪到使用EditText而不是TextView的问题。仅当字段是EditText时才会发生重复调用,而当字段是TextView时系统会表现自己。我在文档或在线中找不到任何内容,表明LineBackgroundSpan无法与EditText一起使用。

我已经更新了MCVE,以显示事物如何与TextView(可以)和EditText(不能-至少不能很好)一起工作。我更新的问题是如何使LineBackgroundSpanEditText一起使用。



我实现了一个简单的类,以便使用EditTextLineBackgroundSpan中的文本上添加圆形背景。一切正常,但是在调试时,我注意到我的类的drawBackground方法被反复调用,而且即使未进行任何更改,字符串中的每个跨度也似乎没有结束。在显示屏上看不出来,但是如果在drawBackground方法中设置了断点,则很容易看出来。

为了找出问题所在,我将代码简化为MCVE。以下代码将突出显示整个文本行。顶行是EditText,底行是TextView。 (这不是我真正想做的,但确实可以达到目的。)

此MCVE在运行API 17和API 24的仿真器以及运行API 24的实际电话上向我展示了这个问题。对于disableDraw的构造函数,将true参数设置为RoundedBackgroudSpan()将会禁用。即使禁用了背景绘图,我仍在drawBackground()上看到问题。

这里发生了什么?我是否误解了如何使用跨度?跨度是否不适用于EditText?任何帮助将不胜感激。

MainActivity.java

    package com.example.bgspanmcve;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.style.LineBackgroundSpan;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import static android.text.Spanned.SPAN_INCLUSIVE_INCLUSIVE;

public class MainActivity extends AppCompatActivity {
    final String dispString = "XAB CD EF";
    private static int count = 0; // times drawBackground is called

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        EditText editText;
        TextView textView;
        RoundedBackgroundSpan bg;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the EditText field with a span.
        // RoundedBackgroundSpan#drawBackground will be called forever for this EditText.
        editText = ((EditText) findViewById(R.id.editText));
        SpannableString ssEditText = new SpannableString(dispString);
        bg = new RoundedBackgroundSpan(INHIBIT_DRAWING, false);
        ssEditText.setSpan(bg, 0, ssEditText.length(), SPAN_INCLUSIVE_INCLUSIVE);
        editText.setText(ssEditText);

        // Set up the TextView field with a span.
        // RoundedBackgroundSpan#drawBackground will be called once for this TextView.
        textView = ((TextView) findViewById(R.id.textView));
        SpannableString ssTextView = new SpannableString(dispString);
        bg = new RoundedBackgroundSpan(INHIBIT_DRAWING, true);
        ssTextView.setSpan(bg, 0, ssTextView.length(), SPAN_INCLUSIVE_INCLUSIVE);
        textView.setText(ssTextView, TextView.BufferType.EDITABLE);
    }

    private static class RoundedBackgroundSpan implements LineBackgroundSpan {
        private boolean mDisableDraw;
        private boolean mIsTextView;

        RoundedBackgroundSpan(boolean disableDraw, boolean isTextView) {
            super();
            mDisableDraw = disableDraw;
            mIsTextView = isTextView;
        }

        @Override
        public void drawBackground(
                Canvas canvas, Paint paint, int left, int right, int top,
                int baseline, int bottom, CharSequence text, int start, int end, int lnum) {

            count++;
            if (mIsTextView) {
                Log.d(TAG, "<<<<drawBackground (TextView) #" + count);
            } else {
                Log.d(TAG, "<<<<drawBackground (EditText) #" + count);
            }
            if (mDisableDraw) return;

            Paint localPaint = new Paint();
            RectF rect = new RectF(left, top, right, bottom);
            localPaint.setColor(BG_COLOR);
            canvas.drawRoundRect(rect, RADIUS_X, RADIUS_Y, localPaint);
        }

        private final String TAG = RoundedBackgroundSpan.class.getSimpleName();
        private final int BG_COLOR = 0xfF00FF00;
        private final int RADIUS_X = 20;
        private final int RADIUS_Y = 20;
    }

    private final static String TAG = MainActivity.class.getSimpleName();
    private final boolean INHIBIT_DRAWING = true;
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.bgspanmcve.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginStart="0dp"
        android:inputType="text"
        android:paddingEnd="0dp"
        android:paddingStart="0dp"
        android:text="EditText"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/editText"
        android:layout_below="@id/editText"
        android:layout_marginStart="0dp"
        android:layout_marginTop="16dp"
        android:paddingEnd="0dp"
        android:paddingStart="0dp"
        android:text="TextView"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

最佳答案

drawBackground()的调用被计时为闪烁光标的速率,如@Suragch建议的那样,约为500毫秒。现在,我确信对drawBackground()的调用是游标实现的一部分。

作为一项快速但不确定的测试,我将EditText字段设置为不显示光标,但仍可编辑(android:cursorVisible="false")。当此属性设置为false时,对drawBackground()的重复调用将停止。

07-24 09:49
查看更多