【问题标题】:Button Counter text animate top to down按钮计数器文本从上到下动画
【发布时间】:2016-09-21 09:42:30
【问题描述】:

我正在构建一个简单的计数器应用程序。我有一个button,它在按下时开始倒计时,倒计时文本也会出现在同一个按钮上。我希望按钮上的倒计时文本在更改为新数字时像从上到下一样动画。 我知道有textview 的解决方案,但我找不到任何按钮的文本。 P.S 我用CountDownTimer类倒计时,这里是代码。

 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
 @SuppressLint("NewApi")

CounterClass timer = new CounterClass(15000,1000);

//some codes--
setContentView(R.layout.activity_main);

play=(Button)findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
            timer.start();
  }
    };
 }

 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")
    public class CounterClass extends CountDownTimer {

        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @SuppressLint("NewApi")
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @Override
        public void onTick(long millisUntilFinished) {

             millis = millisUntilFinished;
            String hms = String.format("%02d:%02d",
                    TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            System.out.println(hms);

            play.setText(hms);
        }

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 你看过侧边栏了吗?根据您提供的信息,我认为他们可以帮助您。

标签: android


【解决方案1】:

一个Android Button,实际上是一个TextView,所以看这个:https://stackoverflow.com/a/24389011/819355

更新:

基于 cmets,这是使用 CountDownTimerTextSwitcher 的可能解决方案(注意:未测试):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center" >
    <Button
        android:id="@+id/play"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <TextView
        android:id="@+id/text1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:clickable="false"
        android:text="Click me" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:clickable="false"
        android:text="" />
</FrameLayout>

代码:

public class MyClass {

    TextSwitcher textSwitcher;

    ...
    CounterClass timer = new CounterClass(15000,1000);
    setContentView(R.layout.activity_main);
    play = (Button) findViewById(R.id.play);
    play.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            timer.start();
        }
    });

    textSwitcher = new TextSwitcher(context);
    textSwitcher.setInAnimation(context, R.anim.slide_in_left); // use any animation
    textSwitcher.setOutAnimation(context, R.anim.slide_out_right);

    textSwitcher.addView(findViewById(R.id.text1));
    textSwitcher.addView(findViewById(R.id.text2));
    ...

    public class CounterClass extends CountDownTimer {

            public CounterClass(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
            }
            public void onTick(long millisUntilFinished) {
                ...
                textSwitcher.setText(hms); // will now animate text
            }
    }
}

【讨论】:

  • 我使用 CountDownTimer 类来倒计时。这是作品吗?
  • 添加了有问题的代码。请看一看。谢谢。我仍然不知道为什么有人投反对票。我找不到任何解决方案。
  • 投反对票可能是因为您尚未发布任何代码或提及您迄今为止尝试过的内容。根据您的代码,我将遵循@Ankit 的回答并使用包装 2 按钮的 TextSwitcher。请参阅:stackoverflow.com/a/7574268/819355(使用 Button 更改 TextView)
  • 我只有一个按钮,这只有在滑动按钮时才有效,我想更改按钮内的文本动画。
  • 如果没有包含文本的视图,您将无法移动文本。您可以创建一个按钮,并在其上创建两个文本视图(使用 RelativeLayout 或 FrameLayout 来定位三个视图),在计数器处于非活动状态时隐藏文本视图,并在计数器启动时通过 TextSwitcher 进行动画处理
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多