2016-02-16 92 views
20

這似乎違反直覺,但有沒有辦法在TextInputLayout中禁用或刪除浮動標籤提示?我想使用TextInputLayout而不僅僅是EditText的原因是TextInputLayout提供的計數器。禁用/刪除TextInputLayout中的浮動標籤提示文本XML

這是我到目前爲止有:

<android.support.design.widget.TextInputLayout 
      android:id="@+id/textContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:scrollbars="vertical" 
      app:counterEnabled="true" 
      app:counterMaxLength="100"> 

      <EditText 
       android:id="@+id/myEditText" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="top|left" 
       android:inputType="textMultiLine|textCapSentences" 
       android:maxLength="100" 
       android:scrollbars="vertical" 
       android:hint="This is my cool hint"/> 

     </android.support.design.widget.TextInputLayout> 
+0

最好的主意,以編程方式,你必須給予提示和textChange事件時間向空提示的編輯框這個想法可以幫助你 –

+0

問題是暗示色彩。輸入內容時會變成白色。請更改提示顏色或更改背景顏色。打字時你會看到提示。 –

回答

0

我認爲這將有助於你:

textContainer.setHintAnimationEnabled(false); 
+0

不工作我的朋友。我試着在代碼中通過XML'app:hintAnimationEnabled =「false」',但都沒有成功。我認爲這只是禁用了浮動標籤的動畫。 – Micro

+0

所以,你想刪除提示文本,是嗎? – Darshak

+0

我希望提示文字出現在「EditText」內部。然後當你點擊'EditText'時,我想讓提示文本消失,而不是移動到浮動標籤。就像常規的「EditText」一樣。 – Micro

0

更新的設計庫V23,並在TextInputLayout

+0

這只是禁用實際的動畫,別無其他...... –

21

有加app:hintAnimationEnabled="false"可能有三種方法可以實現這一點:

TextInputLayout上設置android:hint至空格_字符,並將android:hint="This is my cool hint"設置爲EditText

<android.support.design.widget.TextInputLayout 
    .... 
    .... 
    android:hint=" ">  <<---------- 

    <EditText 
     .... 
     .... 
     android:hint="This is my cool hint"/> <<---------- 

</android.support.design.widget.TextInputLayout> 

這工作,因爲TextInputLayout使用EditText's提示之前進行以下檢查:

// If we do not have a valid hint, try and retrieve it from the EditText 
if (TextUtils.isEmpty(mHint)) { 
    setHint(mEditText.getHint()); 
    // Clear the EditText's hint as we will display it ourselves 
    mEditText.setHint(null); 
} 

通過設置android:hint=" "if (TextUtils.isEmpty(mHint))計算結果爲false,並且EditText保留其暗示。

第二個選項是子類TextInputLayout並覆蓋其addView(View child, int index, ViewGroup.LayoutParams params)方法:

<your.package.name.CTextInputLayout 
    .... 
    .... >  <<---------- 

    <EditText 
     .... 
     .... 
     android:hint="This is my cool hint"/> <<---------- 

</your.package.name.CTextInputLayout> 

public class CTextInputLayout extends TextInputLayout { 

    public CTextInputLayout(Context context) { 
     this(context, null); 
    } 

    public CTextInputLayout(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    public void addView(View child, int index, ViewGroup.LayoutParams params) { 
     if (child instanceof EditText) { 
      // cache the actual hint 
      CharSequence hint = ((EditText)child).getHint(); 
      // remove the hint for now - we don't want TextInputLayout to see it 
      ((EditText)child).setHint(null); 
      // let `TextInputLayout` do its thing 
      super.addView(child, index, params); 
      // finally, set the hint back 
      ((EditText)child).setHint(hint); 
     } else { 
      // Carry on adding the View... 
      super.addView(child, index, params); 
     } 
    } 
} 

然後從設計支持庫使用自定義CTextInoutLayout,而不是一3第三,也許最直接的方法是進行以下調用:

// remove hint from `TextInputLayout` 
((TextInputLayout)findViewById(R.id.textContainer)).setHint(null); 
// set the hint back on the `EditText` 
// The passed `String` could also be a string resource 
((EditText)findViewById(R.id.myEditText)).setHint("This is my cool hinttt."); 
相關問題