Android TimePicker AM/PM button not invoking onTimeChanged

前端 未结 11 1130
半阙折子戏
半阙折子戏 2021-02-05 12:38

I\'m having some issues implementing a TimePicker in my application that allows the user to change the time of a database record prior to inserting it.

The problem is th

11条回答
  •  醉话见心
    2021-02-05 13:31

    I've also faced the same problem using API 21, and the problem hasn't been solved yet.

    I replaced the TimePicker view for a TimePickerDialog and it worked.

    The following code sets a TextView with the time picked from the TimePickerDialog. You can replace the tv.setText() with any logic :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView tv = (TextView)findViewById(R.id.textView);
    
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        tv.setText(String.format(Locale.getDefault(),"Hour: %d, Minute: %d ",hourOfDay,minute));
                    }
        }, hour, minute, true);
        timePickerDialog.show();
    }
    

提交回复
热议问题