简体   繁体   English

设置EditText动画后,键盘不再平移视图

[英]After animating EditText, keyboard no longer pans view

I have an EditText that gets covered by the soft keyboard when it is shown. 我有一个EditText,在显示时,它会被软键盘覆盖。 This particular EditText can also be animated further down the screen when certain UI elements need to be shown before it. 当某些UI元素需要在其之前显示时,还可以将其特定的EditText在屏幕下方进行动画处理。

If the EditText has not been animated, when the keyboard is shown, the entire view gets animated up so that it is no longer covered. 如果EditText尚未设置动画,则在显示键盘时,整个视图将被设置为动画,因此不再被覆盖。 However, after the EditText has been animated down, and the keyboard is shown, the view no longer moves up to uncover the EditText, not even by the amount that it would have taken to uncover it prior to the animation. 但是,在将EditText动画化并显示键盘之后,视图将不再向上移动以显示EditText,甚至不包括在动画之前将其显示所需的数量。

Here is the code I am using to animate the EditText: 这是我用来为EditText设置动画的代码:

findViewById(R.id.myEditText).animate().translationY(100f);

Your wording is a little confusing so I'm gonna try and clarify some stuff to make sure my answer is correct. 您的措辞有点令人困惑,所以我将尝试澄清一些内容以确保我的答案正确。

I'm reading this as, you have a view that is an EditText view. 我正在读这篇文章,因为您有一个EditText视图。 You also have some other UI elements that are sometimes added to the screen, sometimes not. 您还有些其他UI元素有时添加到屏幕上,有时没有添加。 Then you mention "the entire view gets animated up" when the keyboard is shown. 然后,当显示键盘时,您会提到“整个视图变得生动起来”。 I'm assuming you mean the viewGroup? 我假设您的意思是viewGroup? As in, the other UI elements as well as the editText are all in one parent view, or viewGroup like a linear/relative layout or something. 与之类似,其他UI元素以及editText都位于一个父视图或viewGroup中,例如线性/相对布局或其他形式。 If so, your problem could be pretty simple. 如果是这样,您的问题可能非常简单。

Assuming that all the UI elements, as well as the editText are part of a parent view/viewGroup, then your problem could be the translationY(). 假设所有UI元素以及editText都是父view / viewGroup的一部分,那么您的问题可能是translationY()。 Two issues I can see with using it. 使用它可以看到两个问题。 First, if your initial animation moves the editText outside of its parent view, then it could be cut off from the screen. 首先,如果您的初始动画将editText移到其父视图之外,则可以将其从屏幕上切除。 That's an easy fix by adding the following code to its parent view: 通过将以下代码添加到其父视图中,可以轻松解决此问题:

android:clipChildren="false"
android:clipToPadding="false"

The other issue could be that you're moving the editText to a specific position relative to its parent view by using translationY(). 另一个问题可能是您通过使用translationY()将editText移到相对于其父视图的特定位置。 TranslationY(x) moves the view x pixels below its parent view. TranslationY(x)将视图移动其父视图以下x个像素。 So if you've moved the editText's parent view, and then use translationY(100f) it will take the editText to a different location than if you had not moved the parent view. 因此,如果您已经移动了editText的父视图,然后使用了translationY(100f),它将把editText移到与您尚未移动父视图的位置不同的位置。 An alternative could be to use translationYBy(x) which moves the view x pixels from its CURRENT location. 一种替代方法是使用translationYBy(x),将视图x像素从其当前位置移开。

In summary, translationY(100f) moves the view to the location that is 100 pixels below its parent. 总而言之,translationY(100f)将视图移动到其父视图下方100像素的位置。 TranslationYBy(100f) moves the view down by 100 pixels no matter where it is. 无论位置在哪里,TranslationYBy(100f)都会将视图向下移动100个像素。 If you move the view outside the realm if its parent, you'll need to add those two lines I mentioned earlier so that they don't get cut off. 如果将视图移到其父域之外,则需要添加我之前提到的这两行,以免它们被截断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM