简体   繁体   中英

Adding time picker to date picker android

I am trying to add time picker to date picker. I'm beginner at java, maybe somebody could show me the right way ? I want time picker to show up in top of calendar view, of after i choose a date, time picker pop up. Is it possible ? In xml file there is only few EditText fields which are declared already in this class.

 public class CompletedWorksActivity extends Activity implements
        DatePickerDialog.OnDateSetListener, OnClickListener, OnTimeSetListener {
    private TextView text_date;
    private Button search;
    private EditText from;
    private EditText to;
    private int yearfrom;
    private int monthfrom;
    private int dayfrom;
    private int hourfrom;
    private int minfrom;
    private int yearto;
    private int monthto;
    private int dayto;
    private int hourto;
    private int minto;
    private int hour;
    private int min;
    private DatePickerDialog date_picker;
    private TimePickerDialog time_picker;
    private boolean fromEdit;
    private boolean toEdit;

    @Override
    protected void onCreate(Bundle menuinstance) {
        super.onCreate(menuinstance);
        setContentView(R.layout.completed_works);

        Intent intent = getIntent(); // gaunam
        User user = (User) intent.getSerializableExtra("user");

        from = (EditText) findViewById(R.id.datefrom);
        to = (EditText) findViewById(R.id.dateto);
        Calendar cal = Calendar.getInstance();
        hour = cal.get(Calendar.HOUR_OF_DAY);
        min = cal.get(Calendar.MINUTE);
        date_picker = new DatePickerDialog(this, this, cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
        date_picker.getDatePicker().setCalendarViewShown(true);
        date_picker.getDatePicker().setSpinnersShown(false);
        time_picker = new TimePickerDialog(this, this, hour, min, true);
        from.setOnFocusChangeListener(focusListener);
        to.setOnFocusChangeListener(focusListener);
        time_picker.set

        search = (Button) findViewById(R.id.searchSubmit);

        // search.setOnClickListener(searchlistener);
    }

    @Override
    public void onDateSet(DatePicker dp, int y, int m, int d) {
        String cheapDate = y + "-" + (m + 1) + "-" + d;
        View v = getCurrentFocus();
        if (v == from) {
            yearfrom = y;
            monthfrom = m;
            dayfrom = d;
            Log.d("year from ", "" + yearfrom + monthfrom + dayfrom);
        } else {
            yearto = y;
            monthto = m;
            dayto = d;
            Log.d("year to ", "" + yearto + monthto + dayto);
            // Toast.makeText(getApplicationContext(),yearto+monthto+dayto ,
            // Toast.LENGTH_LONG ).show();
        }
        if (fromEdit) {
            from.setText(cheapDate);
        } else {
            to.setText(cheapDate);
        }

    }

    private View.OnClickListener searchlistener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    };

    private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                EditText edit = (EditText) v;
                int len = edit.getText().toString().length();

                if (edit == findViewById(R.id.datefrom)) {
                    Toast.makeText(getApplicationContext(), "Data nuo ? ",
                            Toast.LENGTH_LONG).show();
                    fromEdit = v.getId() == R.id.datefrom;
                    date_picker.show();
                } else {
                    Toast.makeText(getApplicationContext(), "Data iki ? ",
                            Toast.LENGTH_LONG).show();
                    fromEdit = v.getId() == R.id.datefrom;
                    date_picker.show();

                }
            }
        }

    };

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }



}

Android it's very easy to set the time using the android.widget.TimePicker component .

See this Tutorial Here you can see how the user can select the hour, and the minute using the android.app.TimePickerDialog which is an easy to use dialog box.

Try like below code:

date_time_layout.xml

<DatePicker
    android:id="@+id/datePicker1"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:calendarViewShown="false" > <!-- style="@style/date_picker_style" -->
</DatePicker>

<TimePicker
    android:id="@+id/timePicker1"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" >
</TimePicker>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" >

    <Button
        android:id="@+id/btnCancelDT"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/button_selector"
        android:text="Cancel"
        android:textColor="@color/white"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnSetDT"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/button_selector"
        android:text="Set"
        android:textColor="@color/white"
        android:textStyle="bold" />
</LinearLayout>

Activity class:

public void showDateTimePickerDialog() {


        final Dialog dialog = new Dialog(this);

        dialog.setContentView(R.layout.date_time_layout);

        dialog.setTitle("Set Schedule Call");

        dialog.show();

        Button btnCancel = (Button)dialog.findViewById(R.id.btnCancelDT);
        Button btnSet = (Button)dialog.findViewById(R.id.btnSetDT);

        final DatePicker dp = (DatePicker)dialog.findViewById(R.id.datePicker1);
        final TimePicker tp = (TimePicker)dialog.findViewById(R.id.timePicker1);


        btnCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

        btnSet.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String am_pm = "";
                // TODO Auto-generated method stub
                int m = dp.getMonth()+1;
                int d = dp.getDayOfMonth();
                int y = dp.getYear();

                int h = tp.getCurrentHour();
                int min = tp.getCurrentMinute();

                String strm = String.valueOf(min);

                if(strm.length()==1){
                    strm = "0"+strm;
                }
                if(h>12){
                    am_pm = "PM";
                    h = h-12;
                }else{
                    am_pm = "AM";
                }

                date = m+"/"+d+"/"+y+" "+h+":"+strm+":00 "+am_pm;
                time = h+":"+strm+" "+am_pm;

                            System.out.println("Date: " + date +" Time: "+ time);

                dialog.dismiss();
            }
        });
    }

call this method on button click or as you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM