我正在使用TextInputLayout来实现浮动标签模式。但是,当我以编程方式在EditText上设置文本时,仍然会看到标签的动画从控件移动到标签--就像用户单击了它一样。
我不想要这个动画,但是如果我以编程的方式设置它,这有可能吗?这是我的代码:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/root">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1" />
</android.support.design.widget.TextInputLayout>在onResume中,我这样做:
TextInputLayout root = (TextInputLayout)findViewById(R.id.root);
EditText et = (EditText)root.findViewById(R.id.editText1);
et.setText("Actual text");
root.setHint("Hint");发布于 2015-06-21 21:19:57
我找到了一种(笨重的)方法来做那件事。查看TextInputLayout的源代码,我发现类没有用动画更新提示的唯一情况是将EditText添加到其中。唯一的障碍是,您只能将它添加到布局中一次,一旦它在那里,它将永久绑定到它,并且没有办法删除它。
所以解决办法是:
EditText.的情况下创建TextInputLayout 无论是以编程方式还是通过XML膨胀,这都不重要,但它必须是空的。EditText并将其文本设置为您需要的任何内容。EditText添加到TextInputLayout中。下面是一个例子:
TextInputLayout hintView = (TextInputLayout) findViewById(R.id.hint_view);
hintView.setHint(R.string.hint);
EditText fieldView = new EditText(hintView.getContext());
fieldView.setText(value);
hintView.addView(fieldView);不幸的是,如果您想在没有动画的情况下将文本设置为其他内容,那么您将不得不重复所有这些步骤,除了创建一个新的EditText (最后一个可以重用)。我希望谷歌能解决这个问题,因为它真的很不方便,但现在这就是我们所拥有的。
更新:值得庆幸的是,是在设计库23.0.0中修复的,所以只需更新到该版本,您就不必做这些疯狂的事情了。
发布于 2015-08-19 16:51:35
作为支持库的v23,setHintAnimationEnabled method has been added。这是the docs。因此,您可以在XML中将this new attribute设置为false,然后在填充完editText后以编程方式将其设置为true。或者只需根据需要以编程的方式处理。
所以在你的例子中,这会变成这样:
TextInputLayout root = (TextInputLayout)findViewById(R.id.root);
root.setHintAnimationEnabled(false);
root.setHint("Hint");
EditText et = (EditText)root.findViewById(R.id.editText1);
et.setText("Actual text");
// later...
root.setHintAnimationEnabled(true);当然,一定要打开Android,并首先将Android支持库更新为Rev.23,将更新为Rev.17,然后通过以下方式将其添加到build.gradle中:
compile 'com.android.support:design:23.0.0'注意,由于设计库依赖于支持v4和AppCompat支持库,因此在添加设计库依赖项时将自动包含这些库。
发布于 2020-03-15 21:07:16
Put this attribute app:hintEnabled="false" here:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintEnabled="false">对我来说很管用
https://stackoverflow.com/questions/30712338
复制相似问题