vendredi 18 septembre 2015

Display TimePicker when textField is focused

I am just getting into android apps and now trying to work with an interface that allows the user to enter time into a text box. The time picker dialog will display when a text box is focused. But now when I click the text box, the date picker dialog does not display.

WorkDetails.java

public void onFocusChange(View v, boolean hasFocus) {
        EditText txtTime = (EditText) findViewById(R.id.editText6);
        txtTime.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                {
                    TimePick time = new TimePick(v);
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    time.show(ft, "TimePicker");

                }
            }
        });
    }

TimePick.java

@SuppressLint("ValidFragment")
public class TimePick extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
    private TextView time;

    public TimePick(View view)
    {
        time=(EditText)view;
    }


    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        final Calendar c= Calendar.getInstance();
        int hour=c.get(Calendar.HOUR_OF_DAY);
        int minute=c.get(Calendar.MINUTE);

        //Toast.makeText(getActivity(),this,"Date: "+year+"-",+month+"-"+day,Toast.LENGTH_SHORT).show();
        return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view,int hourofDay, int minute)
    {

        time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));



    }


}



from Newest questions tagged java - Stack Overflow http://ift.tt/1im877n
via IFTTT

Aucun commentaire:

Enregistrer un commentaire