質問

I am having a weird problem. When I try to select the time using Time-picker I get a weird result.

For example if I set the time to 11:04 the output is 11:4 ???

Also if I set the time to 12:30 the output is 12:3 ???

//Time Picker Dialog
protected Dialog onCreateDialog(int id)
{
    switch(id)
    {
    case dialog_id:
        return new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
    }       
    return null;
}

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int hour_minute) 
    {
        hour = hourOfDay;
        minute = hour_minute;

        if (hourOfDay > 12) 
        {
            hour = hourOfDay - 12;
            amORpm = "PM";
        } 
        else 
        {
            hour = hourOfDay;
            amORpm = "AM";
        }

        textBox.setText(hour + " : " + minute + " " + amORpm);

        Toast.makeText(getBaseContext(), "Arrival time changed to: " + hour +":" + minute , Toast.LENGTH_LONG).show();

    }
};
役に立ちましたか?

解決 5

Use this Class

public class TimeListener implements OnClickListener, OnTimeSetListener {

    private int hour;
    private int minute;
    private Activity activity;
    private View touchedView;

    public TimeListener(Activity activity) {
        this.activity = activity;
        final Calendar c = Calendar.getInstance();
        this.hour = c.get(Calendar.HOUR_OF_DAY);
        this.minute = c.get(Calendar.MINUTE);
    }

    public int getHour() {
        return hour;
    }

    public int getMinute() {
        return minute;
    }


    @Override
    public void onClick(View v) {
        touchedView = v;

        new TimePickerDialog(activity,
                this, this.getHour(), this.getMinute(), false).show();
    }

    private void updateDisplay() {
        ((TextView) touchedView).setText(
            new StringBuilder()
                    .append(pad(hour)).append(":")
                    .append(pad(minute)));
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        this.hour = hourOfDay;
        this.minute = minute;
        updateDisplay();
    }
}

and use below code Inside your Activity Class...

private TimeListener timeListener = new TimeListener(this);
view.setOnClicklistner(timeListener);

Thank you...

他のヒント

Try using

minute = String.valueOf(hour_minute);

because Integer omits the starting 0, and make sure that minute is a string variable

You need to use below format to show one digit time correctly:

String.format("%02d:%02d", hour, min);

where hour and min represent the value of hour and minutes.

As the return value is an integer, the leading zero is excluded. If you need to make use of the integer, like setting alarm, then you can have it as such. To display to the user, you can get the time from calendar as string and store it in a string which will have leading zero like 02:05 . Getting like this will return the value with zero but as a string:

String.format("%02d", minute);

Use below code it's perfectly working for me.

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            // set time picker as current time
            return new TimePickerDialog(this, timePickerListener, hour,
                    minute, false);
        }
        return null;
    }

    private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
            hour = hourOfDay;
            minute = minutes;
            updateTime(hour, minute);
        }
    };

    private void updateTime(int hours, int mins) {

        String timeSet = "";
        if (hours > 12) {
            hours -= 12;
            timeSet = "PM";
        } else if (hours == 0) {
            hours += 12;
            timeSet = "AM";
        } else if (hours == 12)
            timeSet = "PM";
        else
            timeSet = "AM";

        String minutes = "";
        if (mins < 10)
            minutes = "0" + mins;
        else
            minutes = String.valueOf(mins);

        String aTime = new StringBuilder().append(hours).append(':').append(minutes)
                .append(" ").append(timeSet).toString();
            textBox.setText(aTime);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top