0

カウントダウンするタイマーがあります。表示形式を00.00または「ss.SS」にしたい。しかし、私は何時間も進歩していません。SimpleDateFormat がないと、01.91 と表示されてから 01.9 になります。これにより、ビューを中央に保つためにちらつくため、見るのが難しくなります。私が本当に望んでいるのは、フォーマット 01.90 を維持し、0 を削除できないようにする方法だけです。SimpleDateFormat を使用せずに、元のコードでこれを達成できますか?

/*
 *  This is my original code before I tried the SimpleDateFormat
 *
 *  This code is fully functional and works good, it just keeps dropping the 0 every
 *     10 milliseconds and makes the view shake
 *
 *  getTimeSecs() could return 5, 10, 15, 30, 90 seconds converted to milliseconds
 *  getCountDownInterval() returns 10
 *  
 */

public void createTimer() {
    myCounter = new CountDownTimer(getTimeSecs(), getCountDownInterval()) {
        public void onTick(long millisUntilFinished) {
        timerIsRunning = true;

            if(millisUntilFinished < 10000) {
                TVcountDown.setText("0" + ((millisUntilFinished / 10) / 100.0));
            } else {
                TVcountDown.setText("" + ((millisUntilFinished / 10) / 100.0));
            }
        } //end onTick()

        @Override
        public void onFinish() {  
            timerIsRunning = false;
            TVcountDown.setBackgroundColor(myRes.getColor(R.color.solid_red));
            TVcountDown.setTextColor(myRes.getColor(R.color.white));
            TVcountDown.setText("Expired");

            // Make sure vibrate feature is enabled
            if(wantsVib == true) {
            vib.vibrate(300);
            }

        } //end onFinish()

    }.start();

} //end createTimer()

SimpleDateFormat を試した後のコードは次のとおりです

public void createTimer() {
    myCounter = new CountDownTimer(getTimeSecs(), getCountDownInterval()) {
        public void onTick(long millisUntilFinished) {
        timerIsRunning = true;
            long current = (long) ((millisUntilFinished / 10) / 100.0);
            TVcountDown.setText("" + timerDisplay.format(current));
        }

        @Override
        public void onFinish() {  
            timerIsRunning = false;
            TVcountDown.setBackgroundColor(myRes.getColor(R.color.solid_red));
            TVcountDown.setTextColor(myRes.getColor(R.color.white));
            TVcountDown.setText("Expired");

            // Make sure vibrate feature is enabled
            if(wantsVib == true) {
                vib.vibrate(300);
            }

        }

    }.start();

} //end createTimer()

知っている!私は SimpleDateFormat でそれを手に入れることに近づいているとは思いませんし、イライラしています。実行されますが、ミリ秒側で秒だけカウントダウンします。したがって、15 秒は 15.00 ではなく 00.15 を示します。

誰かが私のためにそれをすべてコーディングすることは期待していません。正しい方向に向けるだけです。私が見つけることができるすべてのチュートリアルには、何年も何日もかかるものがありますが、そこから概念を理解することはできません.

私は SimpleDateFormat を使用しないことを好みます-私にとっては単純ではありませんでした-元のコードを使用して、10ミリ秒ごとにミリ秒側の末尾にゼロを追加します。

前もって感謝します。

4

2 に答える 2

2

これを試して:

TVcountDown.setText(convertToMyFormat(millisUntilFinished));

convertToMyFormat()方法:

public String convertToMyFormat(long ms) {
    String secString, msecString;
    //constructing the sec format:
    int sec = (int) (ms / 1000);
    if(sec < 10)        secString = "0"+sec;
    else if(sec == 0)   secString = "00";
    else                secString = ""+sec;
    //constructing the msec format:
    int msec = (int) ((ms-(sec*1000))/10.0);
    if(msec < 10)       msecString = "0"+msec;
    else if(msec == 0)  msecString = "00";
    else                msecString = ""+msec;

    return secString+":"+msecString;
}

私がそのmsec部分を正しく行ったかどうかはわかりませんが、あなたはあなたが望むようにそれをtweekすることができます。

于 2012-08-18T23:51:08.803 に答える
0

数値を文字列に変換すると、フォーマットが維持されます。さらに、このようなことができます

    public String NumToStr(long i){
    if (i < 10 ) {
        return ("0" + Long.toString(i));
    }
        return Long.toString(i);
    }

「9」が常に「09」として返されるようにします。次に、文字列をテキストに設定します。

実際、これはもっと簡単かもしれません

    if(millisUntilFinished < 10000) {
            TVcountDown.setText("0" + Long.toString((millisUntilFinished / 10) / 100.0));
    } else {
            TVcountDown.setText("" + Long.toString((millisUntilFinished / 10) / 100.0));
    }

Float.toString() または Double.toString など、必要なものを使用してください。必要に応じて、文字列を編集して希望どおりに表示するための小さな関数を書くことを恐れないでください。

  public String KeepFirstTwoCharOfString(String string){
      //code to store first two Char into string
      // return the string containing only first 2 chars
  }
于 2012-08-18T23:00:19.193 に答える