Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 即时更改字体可编辑文本_Android_Android Edittext_Spannable_Android Typeface - Fatal编程技术网

Android 即时更改字体可编辑文本

Android 即时更改字体可编辑文本,android,android-edittext,spannable,android-typeface,Android,Android Edittext,Spannable,Android Typeface,我有一个按钮: if (id == R.id.italic) { } 在那个按钮里面,我有一个可调的: int startSelection = noteContent.getSelectionStart() int endSelection = noteContent.getSelectionEnd(); Spannable spannable = noteContent.getText(); 当用户单击斜体按钮时,我希望它将所选文本设置为可启用斜体。当用户单击同一可编辑文本上的按钮时,我

我有一个按钮:

if (id == R.id.italic) {
}
在那个按钮里面,我有一个可调的:

int startSelection = noteContent.getSelectionStart()
int endSelection = noteContent.getSelectionEnd();
Spannable spannable = noteContent.getText();
当用户单击斜体按钮时,我希望它将所选文本设置为可启用斜体。当用户单击同一可编辑文本上的按钮时,我希望使所选文本正常。

什么if语句将抓住spannable的字体,以便它可以是斜体或非斜体

另一种说法是谷歌在android版谷歌硬盘上实现的斜体特征。我想这样做

例二:

 If (spannable.getTypeface.equals(TypeFace.ITALIC) {


 } else {

 }
当然,上面的if语句代码不是真的,但这正是我想找出的


谢谢

使用此类,您可以设置自定义字体或使用spannable设置您想要的任何字体

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
    super(family);
    newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}
}

你可以这样使用它

  Spannable wordtoSpan = new SpannableString(Text.getText().toString());
            int index = public_desc.getText().toString().indexOf("any string you want");   

            wordtoSpan.setSpan(new CustomTypefaceSpan("", Any type Face), index, text.getText().tostring().lenght, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setText(wordtoSpan);
编辑以将其转换为if语句您可以为选中状态创建一个布尔值,默认为false,单击按钮后反转该布尔值,如果选中等于true,则设置spannable,否则设置另一个字体spannable

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
    super(family);
    newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}
或者你可以用这个来判断字体是粗体还是斜体 用这个

    typefaceB.isBold() && typefaceA.isItalic()



     If (spannable.getTypeface.isItalic()) {


 } else {

 }

Spannable#setSpan
和/或
Spannable#removeSpan
中,什么不起作用?我想要的字体没有问题spannabe@Jordan检查我的更新答案我添加了i way,这样你就可以检查字体是粗体还是斜体。我可以把它转换成if语句吗?这样,如果我再次单击相同的文本,它就会返回正常@Tony这将粗体显示editText中的所有文本,而不仅仅是Spanable,我是否做错了什么@Tonyso你想加粗文本的一部分吗?在Spanable中,你可以通过这个设置开始和结束索引,你可以定义要加粗的区域。OK,check update,我可以做一个像这样的if语句吗?Tony你说了一些关于布尔值的事情。你可以输入一个extampleis字体吗。(布尔字体A示例)@Tony