3

私はアンドロイドスタジオを使って、10秒から始まるカウントダウンを持つアプリに取り組んでいます。私はコードを書きましたが、正常に動作しますが、残りの秒数しか表示されません。カウントダウンにミリ秒も表示する必要があります。助けてください。これがコードです

public class MainActivity extends Activity {
TextView txtCount;
Button btnCount;
int count = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtCount = (TextView)findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.button1);

    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            count++;
            txtCount.setText(String.valueOf(count));
        }
    });
    final TextView textic = (TextView) findViewById(R.id.textView2);

    CountDownTimer Count = new CountDownTimer(10000, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) ((millisUntilFinished / 1000));

            textic.setText(seconds + "seconds " + millisUntilFinished / 1000);

        }

        public void onFinish() {
            textic.setText("TEMPO SCADUTO");
        }
    };

    Count.start();



}
4

4 に答える 4

3

1秒をスキップするCountDownTimerの問題も考慮して、秒とミリ秒でカウントダウンを実装しました( https://stackoverflow.com/a/6811744/1225669 )。わたしにはできる。

private static final long NUMBER_MILLIS = 20000;
private static final String MILLISECONDS_FORMAT = "%03d";
private int secondsLeft = 0;

//
new CountDownTimer(NUMBER_MILLIS, 1) {

        public void onTick(long millisUntilFinished) {                    

                if (Math.round((float)millisUntilFinished / 1000.0f) != secondsLeft)
                {
                    secondsLeft = Math.round((float)millisUntilFinished / 1000.0f);
                }
                long roundMillis = secondsLeft * 1000;
                if(roundMillis==NUMBER_MILLIS){
                    tvTimer.setText(secondsLeft
                            + "." + String.format(MILLISECONDS_FORMAT, 0));
                }else {
                    tvTimer.setText(secondsLeft
                            + "." + String.format(MILLISECONDS_FORMAT, millisUntilFinished % 1000));
                }
        }

        public void onFinish() {

            tvTimer.setText("done!");
        }
}.start();
于 2015-06-10T14:50:07.297 に答える