TimePicker显示不同的时间

时间:2016-10-21 16:00:35

标签: java android timepicker android-timepicker

我制作了editText,用于显示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;
        }
    }

点击保存button后,它会将所选时间传递给AlarmManager,并显示分钟和小时。

 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();
             }

假设我选择时间为11小时,分钟为20并单击保存,正确显示Toast显示1120。但在第二次,我点击保存而不点击时间editTextToast应显示00,但它会显示之前的结果1120

1 个答案:

答案 0 :(得分:1)

我无法确定,因为,一方面,我没有完整的代码,另一方面,我不知道你的意思是“第二次”。

但是你的变量被声明为 static ,这意味着即使你破坏了那个对象,下次你实例化一个新对象时,这些变量也将拥有它们之前的值。实例。这是因为,静态变量在同一个类的所有实例中共享和保留。

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

编辑:

如果您仍然希望以这种方式使用这些变量,则需要先在某处清除它们,但是由您决定最佳位置。

hour1 = 0;
min1 = 0;