android中如何让代码等待动画结束?
我有一个带有 android:maxLength="17" 的 EditText,并使用带有 onClickListener 的按钮来更新此 EditText 的文本,这里的过程是,当文本长度达到 16 时,EditText 动画向左移动,使用 TranslateAnimation(0 , -15, 0, 0);然后 EditText 的第一个字符被删除。为此,我首先调用方法来执行动画,然后调用方法来删除第一个字符,如下所示
animate();
删除字符();
这里的问题是,在动画结束之前,角色被删除,这意味着在动画运行时,removeChar() 完成其功能,而我希望removeChar() 在动画结束后执行其功能。
当我用谷歌搜索这个问题时,我找到了使用动画侦听器的答案,但是在 Eclips 中编写代码时,我没有找到该对象的任何动画侦听器,
这些方法没有错误,并且按照我想要的方式准确地执行其功能。方法的代码如下所示
public void animate()
{
//slide is declared at class level
slide = new TranslateAnimation(0, -15, 0,0 );
slide.setDuration(1250);
slide.setFillAfter(true);
myEditText.startAnimation(slide);
}
public void removeChar()
{
String update="";
for(int i=1 ; i < myEditText.getText().toString().length() ; i++)
{
update=update+input.getText().toString().charAt(i);
}
myEditText.setText(update);
}
我还尝试使用下面的代码来等待动画结束,但它会停止应用程序。
animate();
//slide is declared at class level
while(!(slide.hasEnded()))
{
// Just waste the time
}
removeChar();
我认为应用程序停止是因为所有处理都进行 while 循环,并且动画没有结束,因此循环变成无限循环并导致应用程序停止
我搜索了很多,但没有找到正确的答案,请帮助我,我更喜欢代码片段或者一个小例子,因为我是 android 新手,真的需要帮助......
I have an EditText with android:maxLength="17" and used buttons with onClickListener to update text of this EditText the process here is that when the text length reaches to 16 the EditText animate's towards left with distance of approx one character using TranslateAnimation(0, -15, 0, 0); and then the first character of EditText is being removed. for this first i call the method to perform animation and then call method to remove the the first characer like shown below
animate();
removeChar();
The problem here is that before the end of animation the character got removed, means while the animation is running the removeChar() completes its functionality while i want the removeChar() to perform its functionality after animation ends.
when i googled for this i found answer to use animation listener but while writing code in Eclips i don't found any animation listener for the object
the methods have no error and perform their functionality accurate as i want. the code for methods is show below
public void animate()
{
//slide is declared at class level
slide = new TranslateAnimation(0, -15, 0,0 );
slide.setDuration(1250);
slide.setFillAfter(true);
myEditText.startAnimation(slide);
}
public void removeChar()
{
String update="";
for(int i=1 ; i < myEditText.getText().toString().length() ; i++)
{
update=update+input.getText().toString().charAt(i);
}
myEditText.setText(update);
}
I have also tried to use the code below to wait for end of animation but it halts the application .
animate();
//slide is declared at class level
while(!(slide.hasEnded()))
{
// Just waste the time
}
removeChar();
i think the application halts because all processing goes for while loop and animation doe'nt gots ended and hence loop becomes an infinite loop and causes application to halt
i searched a lot and found no proper answer plz help me and i will prefer a code snippet or a little example as I'm new to android plz really need help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
AnimationListener
附加到您的动画,然后在onAnimationEnd()
方法中执行removeChar()
。像这样:Attach an
AnimationListener
to your animation and then executeremoveChar()
in theonAnimationEnd()
method. Like this: