当以编程方式设置文本时,TextInputLayout 动画与文本重叠
TextInputLayout animation overlaps the text when the text is set programmatically
我正在使用 TextInputLayout
和 设计支持库 22.2.1。
我以编程方式设置 EditText
的值,当屏幕出现时,我可以看到 TextInputLayout
的提示与里面的文本重叠,然后移动到浮动位置。
这里是一个简单的布局:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint1" />
</android.support.design.widget.TextInputLayout>
在我的 Activity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
EditText e1 = (EditText) findViewById(R.id.editText1);
e1.setText("TEXT TEST 1");
}
有人知道解决方法吗?
目前避免此行为的唯一方法是以编程方式添加 EditText
。
- 创建一个没有
EditText
的 TextInputLayout
。以编程方式或通过 XML inflation - 无关紧要,但它必须为空。
- 创建
EditText
并将其文本设置为您需要的任何内容。
- 将
EditTExt
添加到 TextInputLayout
。
这是一个例子:
TextInputLayout til = (TextInputLayout) findViewById(R.id.til);
til.setHint(R.string.hint);
EditText et = new EditText(getContext());
et.setText(value);
til.addView(et);
2015 年 8 月 21 日使用设计库 V23 更新:
使用设计支持库 v23 您可以禁用动画:
只需使用 setHintAnimationEnabled
method:
textInputLayout.setHintAnimationEnabled(boolean)
这里是关于 Google Tracker 的问题。
我最近在使用 DialogFragment 时 运行 遇到了这个问题。要解决这个问题,如果在 设置字段值之前 字段中有值,只需禁用提示动画即可。顺序很重要。
例如,
TextInputLayout layout = (TextInputLayout) findViewById(R.id.text_layout);
TextInputEditText edit = (TextInputEditText) findViewById(R.id.text_edit);
String fieldValue = "Something";
layout.setHintAnimationEnabled(fieldValue == null);
edit.setText(fieldValue);
这样布局不会在设置文本时启动动画。您还可以观察文本更改并在字段为空时再次启用它。
可以直接在XML中指定为TextInputLayout
:
app:hintAnimationEnabled="false"
TextInputLayout seller_email = (TextInputLayout) findViewById(R.id.existing_seller_email_ET);
seller_email.getEditText().setText("Any text");
希望对您有所帮助
我正在使用 TextInputLayout
和 设计支持库 22.2.1。
我以编程方式设置 EditText
的值,当屏幕出现时,我可以看到 TextInputLayout
的提示与里面的文本重叠,然后移动到浮动位置。
这里是一个简单的布局:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint1" />
</android.support.design.widget.TextInputLayout>
在我的 Activity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
EditText e1 = (EditText) findViewById(R.id.editText1);
e1.setText("TEXT TEST 1");
}
有人知道解决方法吗?
目前避免此行为的唯一方法是以编程方式添加 EditText
。
- 创建一个没有
EditText
的TextInputLayout
。以编程方式或通过 XML inflation - 无关紧要,但它必须为空。 - 创建
EditText
并将其文本设置为您需要的任何内容。 - 将
EditTExt
添加到TextInputLayout
。
这是一个例子:
TextInputLayout til = (TextInputLayout) findViewById(R.id.til);
til.setHint(R.string.hint);
EditText et = new EditText(getContext());
et.setText(value);
til.addView(et);
2015 年 8 月 21 日使用设计库 V23 更新:
使用设计支持库 v23 您可以禁用动画:
只需使用 setHintAnimationEnabled
method:
textInputLayout.setHintAnimationEnabled(boolean)
这里是关于 Google Tracker 的问题。
我最近在使用 DialogFragment 时 运行 遇到了这个问题。要解决这个问题,如果在 设置字段值之前 字段中有值,只需禁用提示动画即可。顺序很重要。
例如,
TextInputLayout layout = (TextInputLayout) findViewById(R.id.text_layout);
TextInputEditText edit = (TextInputEditText) findViewById(R.id.text_edit);
String fieldValue = "Something";
layout.setHintAnimationEnabled(fieldValue == null);
edit.setText(fieldValue);
这样布局不会在设置文本时启动动画。您还可以观察文本更改并在字段为空时再次启用它。
可以直接在XML中指定为TextInputLayout
:
app:hintAnimationEnabled="false"
TextInputLayout seller_email = (TextInputLayout) findViewById(R.id.existing_seller_email_ET);
seller_email.getEditText().setText("Any text");
希望对您有所帮助