android中如何让代码等待动画结束?

发布于 2024-12-07 03:45:29 字数 1427 浏览 0 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-12-14 03:45:29

AnimationListener 附加到您的动画,然后在 onAnimationEnd() 方法中执行 removeChar()。像这样:

slide.addAnimationListener(new AnimationListener(){
    public void onAnimationStart(Animation a){}
    public void onAnimationRepeat(Animation a){}
    public void onAnimationEnd(Animation a){
        NameOfParentClass.this.removeChar();
    }

});

Attach an AnimationListener to your animation and then execute removeChar() in the onAnimationEnd() method. Like this:

slide.addAnimationListener(new AnimationListener(){
    public void onAnimationStart(Animation a){}
    public void onAnimationRepeat(Animation a){}
    public void onAnimationEnd(Animation a){
        NameOfParentClass.this.removeChar();
    }

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文