简体   繁体   中英

TimePicker display different time

I have make a editText ,used to display TimePicker .

static Calendar c = null;
static int hour;
static int min;
static int hour1, min1;

 time = (EditText) promptView.findViewById(R.id.time);

  time.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        Fragment fragDialog = getActivity().getSupportFragmentManager().findFragmentByTag("TimePicker");
                        if (fragDialog == null) { // Fragment not added
                            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                            tp.show(ft, "TimePicker");
                        } else {
                            // already active
                        }
                    }
                });

  public static class TimePick extends android.support.v4.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            c = Calendar.getInstance();
            hour = c.get(Calendar.HOUR_OF_DAY);
            min = c.get(Calendar.MINUTE);
            return new TimePickerDialog(getActivity(), this, hour, min, DateFormat.is24HourFormat(getActivity()));
        }


        @Override
        public void onTimeSet(TimePicker view, int hourofDay, int minute) {
            time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
            hour1 = hourofDay;
            min1 = minute;
        }
    }

When save button is clicked, it will pass the selected time to AlarmManager , and display the minute and hour.

 save.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        Calendar alarm = Calendar.getInstance();
                        alarm.set(Calendar.HOUR_OF_DAY, hour1);
                        alarm.set(Calendar.MINUTE, min1);
                        alarm.set(Calendar.SECOND, 0);
                        Toast.makeText(getActivity(), hour1 + "" + min1, Toast.LENGTH_SHORT).show();
                        Intent myIntent = new Intent(getActivity(), MyReceiver.class);
                        pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
                        AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
                        alarmManager.set(AlarmManager.RTC, alarm.getTimeInMillis(), pendingIntent);
                    }
                });

                alert.setView(promptView);
                alert.show();
             }

Assume I select time with hour 11 , min with 20 and click save, the Toast display 1120 correctly. But in second time, I click save without clicking the time editText , the Toast should display 00 but it display the previous result 1120 instead.

I can't tell for sure because, on one hand, I don't have the full code, and on the second hand, I don't know what you mean by 'the second time'.

But you're variables are declared as static , that means that even if you destroy that object, the next time you instantiate a new object, those variables will have the values they had on the previous instance. That is because, static variables are shared and preserved across all instances of the same class.

public class Foo {
    static int bar;
}

Foo a = new Foo();
a.bar = 3;

Foo b = new Foo();
Log.d("tag", "value = " + b.bar); // this will print 3

EDIT:

If you still want to use those variables that way, you need to clean them first, somewhere, but it's up to you to decide the best place.

hour1 = 0;
min1 = 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM