Using timepicker to set time

前端 未结 3 1629
野趣味
野趣味 2021-01-29 00:50

There are two edit text ::

\"enter

Onclick of edittext timepicker

相关标签:
3条回答
  • 2021-01-29 01:21

    Click on your EditText or Button....

    mPickTimeButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // show the time picker dialog
                DialogFragment newFragment = new TimePickerFragment();
                newFragment.show(getSupportFragmentManager(), "timePicker");
            }
        }); // this is on onCreate() method..
    
    @Override
    public void onTimePicked(Calendar time)
    {
        // display the selected time in the TextView
        mPickedTimeText.setText(DateFormat.format("h:mm a", time));
    }
    

    After that create a class TimePickerFragment which is extends with DialogFragment

      public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
    {
      private TimePickedListener mListener;
    
      @Override
     public Dialog onCreateDialog(Bundle savedInstanceState)
      {
        // use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
    
        // create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
      }
    
      @Override
       public void onAttach(Activity activity)
      {
        // when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
        super.onAttach(activity);
        try
        {
            mListener = (TimePickedListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
        }
      }
    
     @Override
     public void onTimeSet(TimePicker view, int hourOfDay, int minute)
     {
        // when the time is selected, send it to the activity via its callback interface method
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
    
        mListener.onTimePicked(c);
     }
    
      public static interface TimePickedListener
     {
        public void onTimePicked(Calendar time);
     }
    }
    
    0 讨论(0)
  • 2021-01-29 01:31

    For opening Time Pcker Dialog you have to put one Button OR You should use one EditText so you can write on click event of Button or EditText...

    You can find example here.

    0 讨论(0)
  • 2021-01-29 01:35

    We can do it smriti3 ,Please try my below sample code and let me know if you found any issues,

     <EditText 
        android:id="@+id/add_TimePicker"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:inputType="none"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textColor="#38251f"                   
        android:focusable="false"/>    
    

    And Add this sample code in within Activity

        Calendar mcurrentDate=Calendar.getInstance();
            int mHour=mcurrentDate.get(Calendar.HOUR_OF_DAY);
            int mMinute=mcurrentDate.get(Calendar.MINUTE);
            EditText mAddTime=(EditText)findViewById(R.id.add_TimePicker);    
            mAddTime.setOnClickListener(new OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub          
    
                            TimePickerDialog mTimePicker=new TimePickerDialog(AddNewMessage.this, new OnTimeSetListener() {                 
                                public void onTimeSet(TimePicker timepicker, int selectedhour, int selectedminute) {
                                    // TODO Auto-generated method stub  
    
                                 mAddTime.setText(selectedhour+":"+selectedminute);
    
                                }
                            },mHour, mMinute,true);
                            mTimePicker.setTitle("Set Time");
                            mTimePicker.show();   
        }
       });
    
    0 讨论(0)
提交回复
热议问题