从Android中的Time Picker中选择时间时启动倒数计时器

时间:2014-05-07 07:33:07

标签: android countdowntimer

我想构建一个应用程序,从时间选择器中选择时间,然后是文本视图中的时间,倒数计时器启动并具有小时,分钟和秒值...... 守则是: -

      public class MainActivity extends Activity implements OnClickListener{
    /** Private members of the class */
    private TextView displayTime;
    private Button pickTime;
    private Button buttonStartTime, buttonStopTime;
    private TextView textViewShowTime; // will show the time
    private CountDownTimer countDownTimer; // built in android class
    private long totalTimeCountInMilliseconds; 
    private int pHour;
    private int pMinute;
    /** This integer will uniquely define the dialog to be used for displaying time picker.*/
    static final int TIME_DIALOG_ID = 0;

    /** Callback received when the user "picks" a time in the dialog */
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                pHour =pHour+ hourOfDay;
                pMinute = pMinute+minute;
                updateDisplay();
                displayToast();
            }
        };

    /** Updates the time in the TextView */
    private void updateDisplay() {
        displayTime.setText(
            new StringBuilder()
                    .append(pad(pHour)).append(":")
                    .append(pad(pMinute)));
 tv.setText(
                    new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mDay).append("-")
                    .append(mMonth + 1).append("-")

                    .append(mYear).append(" "));
    }

    /** Displays a notification when the time is updated */
    private void displayToast() {
        Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(displayTime.getText()),   Toast.LENGTH_SHORT).show();

    }

    /** Add padding to numbers less than ten */
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Capture our View elements */
        displayTime = (TextView) findViewById(R.id.timeDisplay);
        pickTime = (Button) findViewById(R.id.pickTime);

        buttonStartTime = (Button) findViewById(R.id.btnStartTime);
        buttonStopTime = (Button) findViewById(R.id.btnStopTime);
        displayTime = (TextView) findViewById(R.id.timeDisplay);

        buttonStartTime.setOnClickListener(this);
        buttonStopTime.setOnClickListener(this);


        /** Listener for click event of the button */
        pickTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

        /** Get the current time */
        final Calendar cal = Calendar.getInstance();
        mYear = cal.get(Calendar.YEAR);
        mMonth = cal.get(Calendar.MONTH);
        mDay = cal.get(Calendar.DAY_OF_MONTH);
        pHour = cal.get(Calendar.HOUR_OF_DAY);
        pMinute = cal.get(Calendar.MINUTE);

        /** Display the current time in the TextView */
        updateDisplay();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnStartTime) {
           // textViewShowTime.setTextAppearance(getApplicationContext(),
                 //   R.style.normalText);
            setTimer();
            buttonStopTime.setVisibility(View.VISIBLE);
            buttonStartTime.setVisibility(View.GONE);
           //edtTimerValue.setVisibility(View.GONE);
            //edtTimerValue.setText("");
            startTimer();

        } else if (v.getId() == R.id.btnStopTime) {
            countDownTimer.cancel();
            buttonStartTime.setVisibility(View.VISIBLE);
            buttonStopTime.setVisibility(View.GONE);

        }
    }

    private void setTimer() {
        int times = 0;
        if (!displayTime.getText().toString().equals("")) {
           times = Integer.parseInt(displayTime.getText().toString());
            //time=timer;
        } else
            Toast.makeText(MainActivity.this, "Please Enter Minutes...",
                    Toast.LENGTH_LONG).show();

        totalTimeCountInMilliseconds = 60 * times * 1000;

        //timeBlinkInMilliseconds = 30 * 1000;
    }


    private void startTimer() {
        countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
            // 500 means, onTick function will be called at every 500
            // milliseconds

            @Override
            public void onTick(long leftTimeInMilliseconds) {
                long seconds = leftTimeInMilliseconds / 1000;



                displayTime.setText(String.format("%02d:%02d:%02d", seconds / 3600,
                        (seconds % 3600) / 60, (seconds % 60)));
                // format the textview to show the easily readable format

            }

            @Override
            public void onFinish() {
                // this function will be called when the timecount is finished
                displayTime.setText("Time up!");
                displayTime.setVisibility(View.VISIBLE);
                buttonStartTime.setVisibility(View.VISIBLE);
                buttonStopTime.setVisibility(View.GONE);
                //edtTimerValue.setVisibility(View.VISIBLE);
            }

        }.start();

    }

    /** Create a new dialog for time picker */

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, pHour, pMinute, false);
        }
        return null;
    }
}

它给出了无效的int错误:因为文本中的值是' 43:56'那我该怎么解决呢?

1 个答案:

答案 0 :(得分:1)

您不需要从displayTime获取时间。你需要从pHour和pMinute字段中获得时间。 改变这个:

times = Integer.parseInt(displayTime.getText().toString());

到此:

  

次= pHour * 60 + pMinutes;