2015-06-15 35 views
15

我正在使用Android設計庫中的TextInputLayout在EditText上顯示標籤。TextInputLayout提示重疊問題

問題是,當我開始與EditText上提示(標籤)文本活動重疊的實際文本(一秒鐘),然後纔回到自己的位置(在EditText上的頂部)。

爲了說明這個問題,我記錄一個簡短的樣本視頻:https://youtu.be/gy0CzcYggxU

這裏是我的activity.xml:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="16dp" 
    android:orientation="vertical"> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/firstNameTextInputLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="8dp"> 

    <EditText 
     android:id="@+id/firstNameEditText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/first_name" 
     android:inputType="textCapWords" 
     android:textColor="@color/textPrimary" 
     android:textColorHint="@color/textSecondary" 
     android:textSize="16sp" 
     android:theme="@style/CustomEditText"/> 
</android.support.design.widget.TextInputLayout> 


<android.support.design.widget.TextInputLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="24dp"> 

    <EditText 
     android:id="@+id/lastNameEditText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/last_name" 
     android:inputType="textCapWords" 
     android:textColor="@color/textPrimary" 
     android:textColorHint="@color/textSecondary" 
     android:textSize="16sp" 
     android:theme="@style/CustomEditText"/> 
</android.support.design.widget.TextInputLayout> 

+0

我認爲這發生在你回到活動那些具有價值的editext? – Herry

+0

@Herry是的,確切地說。那麼問題在哪裏? –

+0

我不確定問題出在哪裏,但是當您回到活動時,首先由java代碼設置Editext值,然後通過java代碼設置提示並從xml文件中刪除提示僅從java代碼中使用它。 – Herry

回答

1

終於找到了問題的充分的解釋:

那麼事實證明,有一個性能優化添加到 Android 4.0中的框架,該框架允許您的視圖層次結構只有 單次繪製通過,才能開始Activity動畫。一旦 活動動畫結束,您的視圖層次結構按照您的預期每 〜16ms繪製。

瞭解更多:https://medium.com/@chrisbanes

TLDR:它是平臺的限制,會出現此行爲對舊版本(棉花糖和更低)。

在牛軋糖動畫將按預期運行沒有滯後。

2

我想出了這個和其他錯誤廉價的解決方法。

子類TextInputLayout
爲addView見代碼()
如果您在文本充氣時,將設置提示倒塌和防止動畫文本視圖設置。此代碼執行臨時設置文本的解決方法,直到在安裝過程中設置狀態。作爲獎勵,有一些代碼可以確保提示被繪製,以防只有一個佈局通過。

public class TextInputLayout extends android.support.design.widget.TextInputLayout { 

public TextInputLayout(Context context) { 
    super(context); 
} 

public TextInputLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@SuppressLint("DrawAllocation") 
@Override 
protected void onLayout(final boolean changed, final int left, final int top, final int right, final int bottom) { 
    if (ViewCompat.isLaidOut(this)) { 
     super.onLayout(changed, left, top, right, bottom); 
    } else { 
     // Workaround for this terrible logic where onLayout gets called before the view is flagged as laid out. 
     // The normal TextInputLayout is depending on isLaidOut when onLayout is called and failing the check which prevents initial drawing 
     // If there are multiple layout passes this doesn't get broken 
     post(new Runnable() { 
      @SuppressLint("WrongCall") 
      @Override 
      public void run() { 
       TextInputLayout.super.onLayout(changed, left, top, right, bottom); 
      } 
     }); 
    } 
} 

@Override 
public void addView(View child, int index, ViewGroup.LayoutParams params) { 
    if (child instanceof EditText) { 
     EditText editText = (EditText) child; 
     if (StringUtils.isEmpty(editText.getText().toString())) { 
      editText.setText(" "); // Set filler text so the initial state of the floating title is to be collapsed 
      super.addView(child, index, params); 
      editText.setText(""); // Set back to blank to cause the hint to animate in just in case the user sets text 
      // This prevents the hint from being drawn over text that is set programmatically before the state is determined 
      return; 
     } 
    } 
    super.addView(child, index, params); 
} 

}

1

你可以用一個小的延遲編程設置的提示。這不是一個理想的解決方案,但至少它看起來比重疊提示更好。

new Handler().postDelayed(
    new Runnable() { 
     @Override 
     public void run() { 
     textInputLayout.setHint("My hint"); 
     } 
    }, 100 
); 
0

我想這可能是固定的編譯「com.android.support:design:23.0.1」

0

丹尼爾·奧喬亞指出在爲我工作的意見解決方法 - 設置初始狀態帶有一些文本內容的EditText(一個空字符串應該這樣做)。這會強制提示的初始狀態變好。

<android.support.design.widget.TextInputLayout 
    android:id="@+id/firstNameTextInputLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="8dp"> 

    <EditText 
     android:id="@+id/firstNameEditText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/first_name" 
     android:inputType="textCapWords" 
     android:textColor="@color/textPrimary" 
     android:textColorHint="@color/textSecondary" 
     android:textSize="16sp" 
     android:theme="@style/CustomEditText" 
     android:text=" "/> 
</android.support.design.widget.TextInputLayout> 
1

爲我工作的解決方法是這樣的更新活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ... 
    textInputLayout.setHintAnimationEnabled(false); 
    textInput.setText("sample"); 
    textInputLayout.setHintAnimationEnabled(true); 
    ... 
}