简体   繁体   English

android中延迟后如何显示按钮文本

[英]How to display Button text after a delay in android

I created new custom Button class, I want to achieve, whenever user go to any activity my generic button want to expand from circle to default width. 我创建了新的自定义Button类,无论何时用户进行任何活动,我的通用按钮都希望从圆形扩展到默认宽度。 While expanding I want to hide button text for while until button animation complete. 在扩展时,我想隐藏按钮文本一段时间,直到按钮动画完成。

Please check my below code: 请检查我下面的代码:

 private void animateMe(Context context){
    final String btnText = this.getText().toString();
    final FIButton fiButton = this;
    fiButton.setText("");

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            fiButton.setText(btnText);
        }

    },500);


    Animation animation = AnimationUtils.loadAnimation(context,
            R.anim.expand);
    super.startAnimation(animation);
}

Easily by 轻松地

ViewCompat.animate(fiButton ).setStartDelay(500).alpha(1).setDuration(700).setInterpolator(new DecelerateInterpolator(1.2f)).start();

note that you have to set the fiButton alpha to zero android:alpha="0.0" in you xml or on create view 请注意,您必须在XML或创建视图中将fiButton alpha设置为零android:alpha="0.0"

this line will animate your view from 0 to 1 in 700 millisecond after 500 millisecond. 这条线将在500毫秒后的700毫秒内使您的视图从0变为1。

You can use an AnimationListener . 您可以使用AnimationListener On finishing the animation you should do setText on the TextView . 完成动画后,应在TextView上执行setText

private void animateMe(Context context){
    final String btnText = this.getText().toString();
    final FIButton fiButton = this;

    fiButton.setText("");        
    Animation animation = AnimationUtils.loadAnimation(context, R.anim.expand);        
    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            fiButton.setText("");
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            fiButton.setText(btnText);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    super.startAnimation(animation);
}

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

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