java - how to get hour and minute from timepicker in android -
this code got timepicker.
timepicker.java
public class timepicker extends edittext implements android.app.timepickerdialog.ontimesetlistener { public interface ontimesetlistener { public void ontimeset(timepicker view, int hour, int minute); } protected int hour; protected int minute; protected ontimesetlistener ontimesetlistener; protected java.text.dateformat timeformat; public int gethour() { return hour; } public ontimesetlistener getontimesetlistener() { return ontimesetlistener; } public void setontimesetlistener(ontimesetlistener ontimesetlistener) { this.ontimesetlistener = ontimesetlistener; } public void sethour(int hour) { this.hour = hour; updatetext(); } public int getminute() { return minute; } public void setnow() { calendar c = calendar.getinstance(); settime(c.get(calendar.hour_of_day), c.get(calendar.minute)); } public void setminute(int minute) { this.minute = minute; updatetext(); } public void settime(int hour, int minute) { this.hour = hour; this.minute = minute; updatetext(); } public timepicker(context context, attributeset attrs) { super(context, attrs); timeformat = dateformat.gettimeformat(getcontext()); setinputtype(inputtype.type_datetime_variation_date); setfocusable(false); setnow(); } protected void showtimepicker() { timepickerdialog timepickerdialog; timepickerdialog = new timepickerdialog(getcontext(), this, gethour(), getminute(), true); timepickerdialog.show(); } @override public boolean ontouchevent(motionevent event) { if (event.getaction() == motionevent.action_down) showtimepicker(); return super.ontouchevent(event); } public java.text.dateformat gettimeformat() { return timeformat; } public void settimeformat(java.text.dateformat timeformat) { this.timeformat = timeformat; updatetext(); } @override public void ontimeset(android.widget.timepicker view, int hour, int minute) { // todo auto-generated method stub settime(hour, minute); clearfocus(); if (ontimesetlistener != null){ ontimesetlistener.ontimeset(this, hour, minute); } } public void updatetext() { calendar cal = new gregoriancalendar(0, 0, 0, gethour(), getminute()); settext(timeformat.format(cal.gettime())); }
}
calendar_events.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="@color/maroon" tools:context="com.gpplsmje.mac.calendar.calendarevents" > <com.gpplsmje.mac.calendar.utils.timepicker android:id="@+id/alarm_time" style="@android:style/widget.holo.light.spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="60" android:gravity="center" android:textcolor="@color/white" /> </relativelayout>
calendarevents.java
public class calendarevents extends activity implements onclicklistener { timepicker notifytime; button back, cancel, done; int hour, minute, day, month, year; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.calendar_events); onnewintent(getintent()); notifytime = ((timepicker) findviewbyid(r.id.alarm_time)); notifytime.settimeformat(dateformat.gettimeformat(this)); hour = notifytime.gethour(); minute = notifytime.getdminute(); done.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()){ case r.id.bdone: toast.maketext(this, year + " " + month + " " + day + " " + hour + " " + minute , toast.length_long).show(); break; } }
i made toast
message check if have selected timepicker
right thing. every time run code gives me current time on machine , not time selected timepicker
. how accomplish this? can 1 please me? in advance.
use method setontimechangedlistener(timepicker.ontimechangedlistener ontimechangedlistener)
and class when working timepicker. timepicker
not onclick listener on it..
Comments
Post a Comment