2016-09-21 7 views
0

私は簡単なカウンターアプリを構築しています。私は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); 
     } 

助けてください。ありがとう!

+0

あなたはサイドバーを見たことがありますか?あなたが提供する情報で、私は彼らがあなたを助けることができると思う。 – 7geeky

答えて

0

アンアンドロイドButtonは、実際にTextViewあるので、これを参照してください。https://stackoverflow.com/a/24389011/819355

UPDATE:

コメントに基づいて、ここに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 
      } 
    } 
} 
+0

カウントダウンにCountDownTimerクラスを使用しました。この作品は? –

+0

あなたに完全なコードを投稿してください。 – marmor

+0

が問題のコードを追加しました。見てください。ありがとう。私はまだ誰かが否定的な投票をした理由はまだ分かりません。私はこのための任意の解決策を見つけることができません。 –

関連する問題