2016-09-21 2 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

개의 코드를 추가했습니다. 한번 봐주세요. 감사. 왜 누군가가 부정적인 의견을 표명했는지 아직도 모르겠다. 나는 이것에 대한 해결책을 찾지 못한다. –

관련 문제