为什么通过更改字体样式删除下划线?
why underline is removed by changing the style of font?
我是 android 的初学者,所以如果我的问题是 foolish.Basically,请原谅我。我正在尝试制作一个文本编辑器,经过多次尝试,我可以将 bold
、italic
、bold-italic
样式添加到文本没有任何问题。但是现在当我尝试将 underline
添加到文本时 underline
被添加而没有任何错误但是当我将 ex-bold
的样式更改为 bold-italic
然后从最后一个带下划线的字符中删除了下划线[][请看这里underline
下面有bold
文本]
[][现在 underline
通过删除最后一个 underline
]
转移到下一个字符
我的代码:
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);
}
}
}
来自MainActivity.java
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) {
if(under_it) {
under_it = false;
TextArea.underline=false;
editText.changeTypeface(typefaceStyle);
underline.setBackgroundColor(Color.parseColor("#001919"));
}
else {
under_it = true;
TextArea.underline=true;
editText.changeTypeface(typefaceStyle);
underline.setBackgroundColor(Color.parseColor("#fc0505"));
}}
}
}
});
好的,伙计,这是你的解决方案。
检查一下,让我知道这是否有效...
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);
}
}
}
Activity
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);
}
});
我是 android 的初学者,所以如果我的问题是 foolish.Basically,请原谅我。我正在尝试制作一个文本编辑器,经过多次尝试,我可以将 bold
、italic
、bold-italic
样式添加到文本没有任何问题。但是现在当我尝试将 underline
添加到文本时 underline
被添加而没有任何错误但是当我将 ex-bold
的样式更改为 bold-italic
然后从最后一个带下划线的字符中删除了下划线[underline
下面有bold
文本]
[underline
通过删除最后一个 underline
]
我的代码:
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);
}
}
}
来自MainActivity.java
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) {
if(under_it) {
under_it = false;
TextArea.underline=false;
editText.changeTypeface(typefaceStyle);
underline.setBackgroundColor(Color.parseColor("#001919"));
}
else {
under_it = true;
TextArea.underline=true;
editText.changeTypeface(typefaceStyle);
underline.setBackgroundColor(Color.parseColor("#fc0505"));
}}
}
}
});
好的,伙计,这是你的解决方案。
检查一下,让我知道这是否有效...
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);
}
}
}
Activity
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);
}
});