开发者

how to make code wait untill an animation ends in 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 dec开发者_运维知识库lared 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...


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();
    }

});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜