有没有办法在文本中找到 "superscript spanned strings" 存在的地方,并将它们重新显示为粗体?

Is there a way to find in a text the places where the "superscript spanned strings" are existing, and respan them as bold?

有没有办法在 TextView 中找到“上标跨越字符串”存在的地方,并将它们重新设为粗体?

例如index(start:13, end 15), index(start 25, end 33)等。然后对它们重新应用粗体跨度。有什么想法吗?

AFAIK 您无法直接获得 TextView.

的特定样式部分(上标

要解决这个问题,您需要在制作上标时保存 start/end 索引,并在需要加粗时再次使用它们。

您可以有一个 data/pojo class 来存储:id、startIndex、endIndex、styleType...等

然后你可以使用这些数据class建立一个Room数据库。

您可以check documentation to find out how to build up a Room database, also you can check this获得完整示例

不过这里我给个骨架

Data/Pojo class

@Entity(tableName = "spannable_data")
public class SpannableData {

    @PrimaryKey(autoGenerate = true)
    int id;
    int styleType;
    int startIndex;
    int endIndex;

    @Ignore
    public SpannableData(int styleType, int startIndex, int endIndex) {
        this.styleType = styleType;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    public SpannableData(int id, int styleType, int startIndex, int endIndex) {
        this.id = id;
        this.styleType = styleType;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStyleType() {
        return styleType;
    }

    public void setStyleType(int styleType) {
        this.styleType = styleType;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }
}

道class

@Dao
public
interface SpannableDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertSpan(SpannableData spannableData);

    @Query("SELECT * FROM spannable_data WHERE styleType = :type")
    List<SpannableData> getSpans(int type);

    @Query("DELETE FROM spannable_data WHERE styleType = :type")
    void deleteSpans(int type);

    @Query("DELETE FROM spannable_data")
    void deleteAllSpans();

    @Delete
    void deleteSpan(SpannableData spannableData);

}

数据库class

@Database(entities = {SpannableData.class}, version = 1, exportSchema = false)

public abstract class SpannableDatabase extends RoomDatabase {

    public static final String DATABASE_NAME = "MyDatabase.db";

    private static volatile SpannableDatabase INSTANCE;

    // used from synchronization (to avoid any race conditions) so that make sure that two parts of the app don’t try to create the database at the same time
    private static final Object LOCK = new Object();

    public abstract SpannableDao getSpannableDao();

    static public SpannableDatabase getInstance(final Context context) {
        if (INSTANCE == null) {
            synchronized (LOCK) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            SpannableDatabase.class, DATABASE_NAME)
                            .build();
                }
            }
        }
        return INSTANCE;
    }

}