Android 暗示中的不同颜色
我正在使用Android 暗示中的不同颜色,android,android-layout,android-edittext,android-textinputlayout,Android,Android Layout,Android Edittext,Android Textinputlayout,我正在使用TextInputLayout作为EditText中的提示 现在,我想用两种颜色提示TextInputLayout 如图所示: 这可能吗?我尝试过添加html格式的文本,但它不起作用 编辑文本的代码: <android.support.design.widget.TextInputLayout android:id="@+id/pv" android:layout_width="match_parent"
TextInputLayout
作为EditText
中的提示
现在,我想用两种颜色提示TextInputLayout
如图所示:
这可能吗?我尝试过添加html格式的文本,但它不起作用
编辑文本的代码:
<android.support.design.widget.TextInputLayout
android:id="@+id/pv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Property Value"
android:layout_marginTop="5dp"
app:hintTextAppearance="@style/HintText"
>
<com.app.aspirehomeloans.FontClass.Edittext_RobotoRegular
android:id="@+id/login_emailid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="@dimen/edittext_size"
android:paddingTop="3dp"
android:imeOptions="actionNext"
android:paddingBottom="3dp"
android:background="@android:color/transparent"
android:inputType="textNoSuggestions|textEmailAddress"
android:textColor="@color/edittextcolor"
android:textCursorDrawable="@null"
android:text=""
android:nextFocusDown="@+id/login_password"
/>
<View
android:id="@+id/login_email_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/divercolor"
/>
</android.support.design.widget.TextInputLayout>
您可以使用以下方法来实现这一点:
#1首先在xml
文件中设置提示,例如:“属性值”
#2然后使用此方法在提示中添加红色星号:
private void addRedAsterisk(EditText editText){
String text = editText.getHint().toString();
String asterisk = " *";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(text);
int start = builder.length();
builder.append(asterisk);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(builder);
}
更新
当您使用TextInputLayout
时,此方法将无法正常工作
当前,TextInputLayout
不支持在提示中设置不同的颜色
但是,您可以使用以下类获得部分结果,即,当提示处于展开状态时,您将获得红色星号,但当它处于折叠状态时,则不会得到:
public class HintColorHelper {
public static void setUpHintColor(EditText editText, TextInputLayout textInputLayout){
textInputLayout.setHintTextAppearance(0);
String hint = textInputLayout.getHint().toString();
final SpannableStringBuilder hintWithAsterisk = getHintWithAsterisk(hint);
if(!editText.hasFocus()){
textInputLayout.setHint(null);
editText.setHint(hintWithAsterisk);
}
setOnFocuschangeListener(editText, textInputLayout, hintWithAsterisk);
}
private static void setOnFocuschangeListener(final EditText editText, final TextInputLayout textInputLayout, final SpannableStringBuilder hintWithAsterisk){
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
textInputLayout.setHint(hintWithAsterisk);
editText.setHint(null);
} else if(editText.getText().toString().length()==0){
textInputLayout.setHint(null);
editText.setHint(hintWithAsterisk);
}
}
});
}
private static SpannableStringBuilder getHintWithAsterisk(String hint){
String asterisk = " *";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(hint);
int start = builder.length();
builder.append(asterisk);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return builder;
}
}
要使用它,只需使用HintColorHelper
类的静态方法setUpHintColor(editText,textInputLayout)
,如下所示:
HintColorHelper.setUpHintColor(yourEditText, yourTextInputLayout);
以下是输出:
您可以使用以下方法来实现这一点:
#1首先在
xml
文件中设置提示,例如:“属性值”
#2然后使用此方法在提示中添加红色星号:
private void addRedAsterisk(EditText editText){
String text = editText.getHint().toString();
String asterisk = " *";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(text);
int start = builder.length();
builder.append(asterisk);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(builder);
}
更新
当您使用TextInputLayout
时,此方法将无法正常工作
当前,TextInputLayout
不支持在提示中设置不同的颜色
但是,您可以使用以下类获得部分结果,即,当提示处于展开状态时,您将获得红色星号,但当它处于折叠状态时,则不会得到:
public class HintColorHelper {
public static void setUpHintColor(EditText editText, TextInputLayout textInputLayout){
textInputLayout.setHintTextAppearance(0);
String hint = textInputLayout.getHint().toString();
final SpannableStringBuilder hintWithAsterisk = getHintWithAsterisk(hint);
if(!editText.hasFocus()){
textInputLayout.setHint(null);
editText.setHint(hintWithAsterisk);
}
setOnFocuschangeListener(editText, textInputLayout, hintWithAsterisk);
}
private static void setOnFocuschangeListener(final EditText editText, final TextInputLayout textInputLayout, final SpannableStringBuilder hintWithAsterisk){
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
textInputLayout.setHint(hintWithAsterisk);
editText.setHint(null);
} else if(editText.getText().toString().length()==0){
textInputLayout.setHint(null);
editText.setHint(hintWithAsterisk);
}
}
});
}
private static SpannableStringBuilder getHintWithAsterisk(String hint){
String asterisk = " *";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(hint);
int start = builder.length();
builder.append(asterisk);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return builder;
}
}
要使用它,只需使用HintColorHelper
类的静态方法setUpHintColor(editText,textInputLayout)
,如下所示:
HintColorHelper.setUpHintColor(yourEditText, yourTextInputLayout);
以下是输出:
试试这个代码
String text = "<font color=#307575>Property</font> <font color=#bdd95b> Value</font>";
TextInputLayout textInputLayoutObj = (TextInputLayout)findViewById(R.id.text_input_layout_id);
textInputLayoutObj .setHint(Html.fromHtml(text));
String text=“属性值”;
TextInputLayout textInputLayoutBj=(TextInputLayout)findViewById(R.id.text\u input\u layout\u id);
setHint(Html.fromHtml(text));
试试这个代码
String text = "<font color=#307575>Property</font> <font color=#bdd95b> Value</font>";
TextInputLayout textInputLayoutObj = (TextInputLayout)findViewById(R.id.text_input_layout_id);
textInputLayoutObj .setHint(Html.fromHtml(text));
String text=“属性值”;
TextInputLayout textInputLayoutBj=(TextInputLayout)findViewById(R.id.text\u input\u layout\u id);
setHint(Html.fromHtml(text));
如果您不能使用xml,您需要以编程方式进行,我建议您使用Spanabble字符串
TextView textvw = (TextView)findViewById(R.id.mytext);
String txthint = "Property value ";
String astrk= "*";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(txthint);
int start = builder.length();
builder.append(astrk);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textvw .setText(builder);
如果您不能使用xml来实现这一点,那么您需要通过编程来实现,我建议您使用Spanabble字符串
TextView textvw = (TextView)findViewById(R.id.mytext);
String txthint = "Property value ";
String astrk= "*";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(txthint);
int start = builder.length();
builder.append(astrk);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textvw .setText(builder);
但是我已经将提示设置为InputExtLayout,当我使用TextInputLayout时,您可以使用TextInputLayout
而不是上面代码中的EditText
,然后它就不会改变了。只添加了一个*。没有不同的颜色。我已经使用了您的代码,但我已将提示设置为InputExtLayout,当我使用TextInputLayout时,您可以在上面的代码中使用TextInputLayout
而不是EditText
,然后它不会发生变化。只添加了一个*。没有不同的颜色。我用了你的密码,但没用。我已经试过了。正在向textinputlayout添加提示。是,这不起作用。请自己尝试一下,告诉我它是否有效,然后将xml代码和java代码发送给我。Please@ParthAnjaria好的,不行。我已经试过了。正在向textinputlayout添加提示。是,这不起作用。请自己尝试一下,告诉我它是否有效,然后将xml代码和java代码发送给我。Please@ParthAnjaria好的,如果我没有错的话,你正在尝试实现用户输入文本颜色和提示颜色的差异。对吗?我正在尝试在一个提示中实现两种不同的颜色。如图中所示,“构建区域(SQFT)”的颜色不同,“*”的颜色也不同。您是否尝试过我的答案?如果我没有错,您正在尝试实现用户输入文本颜色和提示颜色的差异。对吗?我尝试在一个提示中实现两种不同的颜色。如图中所示,“建筑面积(SQFT)”的颜色不同,“*”的颜色也不同。您是否尝试过我的答案?它不起作用@Aditya。因为我需要向InputExtLayout而不是edittext添加提示,否则如果我需要向textviewit添加文本,则此代码是有效的@Aditya。因为我需要向InputExtLayout而不是edittext添加提示,否则,如果我需要向textview添加文本,则此代码是有效的