为什么通过改变字体的样式删除下划线?

时间:2018-01-22 15:36:44

标签: android android-edittext text-styling

我是android的初学者,所以请原谅我,如果我的问题是愚蠢的。基本上我正在尝试制作一个文本编辑器,经过大量的尝试后我可以添加bolditalicbold-italic样式文字没有任何问题。但是现在,当我尝试将underline添加到文字underline时,添加时没有任何错误,但是当我将ex [{1}}的样式更改为bold时,从最后带下划线的字符[]中移除了下划线[见bold-italic underline文字下方{/ 1>}

[] [现在bold通过移除上一个underline]

转移到下一个字符

我的代码:

underline

来自MainActivity.java

public class TextArea extends EditText {

public static final int TYPEFACE_NORMAL = 0;
public static final int TYPEFACE_BOLD = 1;
public static final int TYPEFACE_ITALICS = 2;
public static final int TYPEFACE_BOLD_ITALICS = 3;
public static final boolean underline=false;

private int currentTypeface;
private int lastCursorPosition;
private int tId;


public TextArea(Context context) {
    super(context);
    lastCursorPosition = this.getSelectionStart();
}

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


public int gettId() {
    return tId;
}

public void settId(int tId) {
    this.tId = tId;
}

public void changeTypeface(int tfId) {
    currentTypeface = tfId;
    lastCursorPosition = this.getSelectionStart();
}

@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
    Spannable str = this.getText();
    StyleSpan ss;UnderlineSpan tt = new UnderlineSpan();
    int endLength = text.toString().length();

    switch(currentTypeface) {
        case TYPEFACE_NORMAL:
            ss = new StyleSpan(Typeface.NORMAL);
            break;
        case TYPEFACE_BOLD:
            ss = new StyleSpan(Typeface.BOLD);
            break;
        case TYPEFACE_ITALICS:
            ss = new StyleSpan(Typeface.ITALIC);
            break;
        case TYPEFACE_BOLD_ITALICS:
            ss = new StyleSpan(Typeface.BOLD_ITALIC);
            break;
        default:
            ss = new StyleSpan(Typeface.NORMAL);
    }
    if(underline){
    str.setSpan(ss, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    str.setSpan(tt, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}   
    else{
 str.setSpan(ss, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}

1 个答案:

答案 0 :(得分:1)

好的伙伴,这是你的解决方案。

检查并告诉我这是否有效......

TextArea.java

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

    public static final int TYPEFACE_NORMAL = 0;
    public static final int TYPEFACE_BOLD = 1;
    public static final int TYPEFACE_ITALICS = 2;
    public static final int TYPEFACE_BOLD_ITALICS = 3;
    public static final int TYPEFACE_UNDERLINE = 4;
    public static boolean underline = false;

    private int currentTypeface;
    private int lastCursorPosition;
    private int tId;


    public TextArea(Context context) {
        super(context);
        lastCursorPosition = this.getSelectionStart();
    }

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


    public int gettId() {
        return tId;
    }

    public void settId(int tId) {
        this.tId = tId;
    }

    public void changeTypeface(int tfId) {
        currentTypeface = tfId;
        lastCursorPosition = this.getSelectionStart();
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        Spannable str = this.getText();
        StyleSpan ss;
        UnderlineSpan tt = new UnderlineSpan();
        int endLength = text.toString().length();
        underline = false;

        Log.d("onTextChanged ", text.toString());

        switch (currentTypeface) {
            case TYPEFACE_NORMAL:
                ss = new StyleSpan(Typeface.NORMAL);
                break;
            case TYPEFACE_BOLD:
                ss = new StyleSpan(Typeface.BOLD);
                break;
            case TYPEFACE_ITALICS:
                ss = new StyleSpan(Typeface.ITALIC);
                break;
            case TYPEFACE_UNDERLINE:
                underline = true;
                ss = new StyleSpan(Typeface.NORMAL);
                tt = new UnderlineSpan();
                break;
            case TYPEFACE_BOLD_ITALICS:
                ss = new StyleSpan(Typeface.BOLD_ITALIC);
                break;
            default:
                ss = new StyleSpan(Typeface.NORMAL);
        }
        if (underline) {
//            str.setSpan(ss, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            str.setSpan(tt, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            // Runtime Error Fix!
            if (endLength > lastCursorPosition)
                str.setSpan(ss, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

活动

    bold = findViewById(R.id.bold);
    italic = findViewById(R.id.italic);
    boldItalic = findViewById(R.id.boldItalic);
    editText = findViewById(R.id.editText);
    normal = findViewById(R.id.normal);
    underline = findViewById(R.id.underline);
    typefaceStyle = TextArea.TYPEFACE_NORMAL;
    editText.changeTypeface(typefaceStyle);

    normal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            typefaceStyle = TextArea.TYPEFACE_NORMAL;
            editText.changeTypeface(typefaceStyle);
        }
    });


    bold.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            typefaceStyle = TextArea.TYPEFACE_BOLD;
            editText.changeTypeface(typefaceStyle);
        }
    });

    italic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            typefaceStyle = TextArea.TYPEFACE_ITALICS;
            editText.changeTypeface(typefaceStyle);
        }
    });

    boldItalic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            typefaceStyle = TextArea.TYPEFACE_BOLD_ITALICS;
            editText.changeTypeface(typefaceStyle);

        }
    });


    underline.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            typefaceStyle = TextArea.TYPEFACE_UNDERLINE;
                editText.changeTypeface(typefaceStyle);

        }
    });