0

我在EditText中使用TextInputLayout作爲提示。提示中的不同顏色

現在,我想給兩種顏色的TextInputLayout提示。

像圖所示:

enter image description here

這可能嗎?我曾嘗試以html格式添加文本,但它不起作用。

代碼爲EditText上:

<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> 
+0

如果我沒有錯,你正試圖實現用戶輸入的文本顏色和提示顏色不同..好嗎? –

+0

我想在一個提示中達到2種不同的顏色。像在圖像「構建區(SQFT)」中看到的是不同的顏色和「*」是在不同的顏色 –

+0

你試過我的回答? –

回答

0

您可以使用SpannableString實現這一目標,這樣的事情應該工作:

#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); 
} 

UPDATE

由於您使用的是TextInputLayout此方法將無法正常工作 目前在提示設置不同的顏色TextInputLayout不支持。

但是,你可以使用下面的類,即部分結果,你會得到紅色星號時hint處於擴張狀態而不是在它被摺疊:

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); 

下面是輸出:

+0

但我已將提示設置爲InputTextLayout –

+0

,那麼您可以在上面的代碼中使用'TextInputLayout'而不是'EditText' –

+0

當我使用TextInputLayout時,它會改變。只增加一個*。沒有不同的顏色。我已使用您的代碼 –

2

試試這個代碼

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)); 
+0

無法正常工作。已經試過了。將提示添加到textinputlayout。 –

+0

你有沒有試過以上更新的。 –

+0

是的,這是行不通的。請自己嘗試一下,告訴我它是否可以正常工作,然後向我發送xml代碼以及java代碼。請 –

0

據我所知,你不能這樣做,使用XML,你需要做的是編程,我建議你使用Spanabble String

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); 
+0

它不工作@Aditya。因爲我需要添加提示InputTextLayout不edittext,否則此代碼是functinal如果我neet文本添加到textview –