android - Android TextView 中的强制星号

标签 android textview

我想使用包含红色星号的 TextView 显示必填字段

My Output

Required Output

我尝试修改代码,但没有得到预期的结果。

我的代码:

public void setCompulsoryAsterisk() {
    txt_name="Name : ";
    String colored="*";
    SpannableStringBuilder strBuilder=new SpannableStringBuilder();
    strBuilder.append(txt_name);
    int start=strBuilder.length();
    strBuilder.append(colored);
    int end=strBuilder.length();
    strBuilder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    txtName.setText(strBuilder);
}

最佳答案

尝试这样的事情:

string = "<font color='#000000'>Name </font>" + "<font color='#FF0000'>*</font>" + "<font color='#000000'>:</font>";
textView.setText(Html.fromHtml(string));

关于android - Android TextView 中的强制星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27243273/

相关文章:

android - Loader 和 LoaderManager - 如何判断加载是否出错

android - 如何创建像TextView和EditText这样的Whatsapp?

android - 如何使用android中的任何布局将两个 TextView 与背景水平对齐?

android - 具有透明图像的 CSS border-image 在 Android 上显示边缘

android - 副作用问题 - Jetpack Compose 中的 LaunchedEffect 和 SideEffect

android - ViewPager 类似于 AndroidTV 中 BrowseFragment 中的东西

android - 如何制作自定义 TextView?

android - Activity 上的简单确定按钮

android - 如何将 TextView 添加到 AppCompat PreferenceActivity

textview - 如何在它的末尾创建带有 cross(x) 按钮的 TextView?