本文介绍了如何显示在android的斜体文本字段的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序这需要从文本字段中的某些数据由用户输入,输出基于该数据的新文本字段一个字符串。很简单,但是我需要的字符串的部分是斜体。

I am developing an Android app which takes some data from text fields entered by the user, and outputs a single string in a new text field based on this data. Simple enough, however I need parts of the string to be in italics.

我已经找到了如何让整个文本字段斜体如何选择只有特定的变量,弥补字符串以斜体字显示?显示(如下图所示),但如何

I have found out how to make the entire text field display in italics (shown below) but how about choosing only specific variables which make up the string to be shown in italics?

public class RefGenActivity extends Activity {

private EditText reftext;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_refgen);
reftext = (EditText) findViewById(R.id.editText1);
reftext.setTypeface(null, Typeface.ITALIC); //SET TO ITALICS
reftext.setText(getRef());
}

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

推荐答案

您可以使用Spannable字符串

You can use a Spannable String

String s= "Hello Everyone";
  SpannableString ss1=  new SpannableString(s);
  ss1.setSpan(new RelativeSizeSpan(2f), 0,5, 0);
  ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);
  ss1.setSpan(new StyleSpan(Typeface.ITALIC), 0, 5, 0);
  ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
  ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, ss1.length(), 0);
  editText.append(ss1);

有关更多造型

这篇关于如何显示在android的斜体文本字段的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:58