Android TimePicker AM/PM button not invoking onTimeChanged

前端 未结 11 1123
半阙折子戏
半阙折子戏 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:37

    Please use the below code it is working fine for me.

    final TimePicker tp=(TimePicker)findViewById(R.id.timePicker1);
        View amPmView  = ((ViewGroup)tp.getChildAt(0)).getChildAt(2);
        if(amPmView instanceof Button)
        {
            amPmView.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Log.d("OnClickListener", "OnClickListener called");
                    if(v instanceof Button)
                    {
                        if(((Button) v).getText().equals("AM"))
                        {
                            ((Button) v).setText("PM");
                             if (tp.getCurrentHour() < 12) {
                                 tp.setCurrentHour(tp.getCurrentHour() + 12);
                                }  
    
                        }
                        else{
                            ((Button) v).setText("AM");
                             if (tp.getCurrentHour() >= 12) {
                                 tp.setCurrentHour(tp.getCurrentHour() - 12);
                                }
                        }
                    }
    
                }
            });
        }
    

提交回复
热议问题