0

カウントダウンは機能していますが、入力したハードコードされた整数からカウントダウンするだけです。ユーザーが数値を入力して、代わりにその数値からカウントダウンできるようにしたいと考えています。「timeedit」に入れられたテキストを文字列に入れて「startTime」の値に入れたいと思います。

編集: 以下のコードが画面上で正しくインデントされていない場合は、ここでコードを表示することもできます: http://pastebin.com/BnzEtFX5

コード:

public class TimerActivity extends Activity implements OnClickListener {

    private CountDownTimer countDownTimer;
    private boolean timerHasStarted = false;
    private Button startB;
    public TextView text;
    public String time;
    private long startTime = 30 * 1000;
    private final long interval = 1 * 1000;
    private EditText timeedit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_countdown);
        startB = (Button) this.findViewById(R.id.button);
        startB.setOnClickListener(this);
        text = (TextView) this.findViewById(R.id.timer);
        timeedit = (EditText) findViewById(R.id.timeedit);
        countDownTimer = new MyCountDownTimer(startTime, interval);
        time = timeedit.getText().toString();
        text.setText(time); //+ String.valueOf(startTime/1000)
    }

    @Override
    public void onClick(View v) {
        if (!timerHasStarted) {
            countDownTimer.start();
            timerHasStarted = true;
            startB.setText("STOP");
        } else {
            countDownTimer.cancel();
            timerHasStarted = false;
            startTime = 30 * 1000;
            startB.setText("RESTART");
        }
    }


    public class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            text.setText("Time's up!");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            text.setText("" + millisUntilFinished / 1000);
        }
    }
}
4

2 に答える 2

1

うーん...何が問題なのかわかりませんが、あなたのことを正しく理解していれば、次のようなことをする必要がありますonCreate()

String userTime = timeedit.getText().toString();
startTime = Long.parseLong(userTime);

編集

startTime実際にはとuserTimeをに入れたいと思うでしょうonClick。それが入っている場合、xmlにデフォルト値を入れない限り空になりますが、いずれにしてもユーザーが入力したものにはなりませんonCreate()。また、ユーザーが数字以外の文字を入力した場合にEditText備えて、a で囲む必要があります。try/catch

 public void onClick(View v) {
    String userTime = timeedit.getText().toString();
    long startTime = Long.parseLong(userTime);
    if (!timerHasStarted) {
        countDownTimer.start();
        timerHasStarted = true;
        startB.setText("STOP")
于 2013-05-27T03:17:26.697 に答える
0

ユーザーに何らかの入力をさせたい場合は、次のようにして取得するだけです。

    String userCountDown = timeEdit.getText().toString();

次に、それを Long に解析します。

    long userStartTime = Long.parseLong(userCountDown);

そして、それをcountDownに渡します

    countDownTimer = new MyCountDownTimer(UserStartTime, interval);

ただし、timeEdit 内の値が Long 値であることを確認する必要があります。layout.xml ファイル内の属性 inputType="number" を R.id.timeedit に設定することで、確実に Long を取得できます。ただし、入力があることを確実にするために、任意のチェックを実行して、ユーザーに警告を表示できます。

        if (!timerHasStarted) { 
              String userCountDown = timeEdit.getText().toString();
             if(userCountDown.length()<1){

                Toast.makeText(yourActivity.this,"PLEASE DO SOME INPUT",Toast.LENGTH_LONG).show();
              }else{
                 long userStartTime = Long.parseLong(userCountDown);
                    countDownTimer = new MyCountDownTimer(userStartTime, interval);
                countDownTimer.start();
              timerHasStarted = true;
              startB.setText("STOP");
            }
       } else {

             countDownTimer.cancel();
             timerHasStarted = false;
              startTime = 30*1000;
              startB.setText("RESTART");
         }
于 2013-05-27T03:26:50.097 に答える