Date and Time Picker
Picker
View
Salah satu View UI (User Interface) adalah Picker View. Memilih tanggal dan waktu adalah salah satu tugas yang sangat umum yang mungkin anda lakukan dalam aplikasi mobile. Android mendukung fungsi ini melalui TimePicker dan DatePicker.
1. Date and Time Picker
main.xml
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/datepickerbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="DatePicker" /> <Button android:id="@+id/timepickerbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TimePicker" /> </LinearLayout> |
DateTimePicker.java package android.dateTimePicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class DateTimePicker extends Activity {
private int myYear, myMonth, myDay, myHour, myMinute;
static final int ID_DATEPICKER = 0;
static final int ID_TIMEPICKER = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button datePickerButton = (Button)findViewById(R.id.datepickerbutton);
Button timePickerButton = (Button)findViewById(R.id.timepickerbutton);
datePickerButton.setOnClickListener(datePickerButtonOnClickListener);
timePickerButton.setOnClickListener(timePickerButtonOnClickListener);
}
private Button.OnClickListener datePickerButtonOnClickListener
= new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
myYear = c.get(Calendar.YEAR);
myMonth = c.get(Calendar.MONTH);
myDay = c.get(Calendar.DAY_OF_MONTH);
showDialog(ID_DATEPICKER);
}
};
private Button.OnClickListener timePickerButtonOnClickListener
= new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
myHour = c.get(Calendar.HOUR_OF_DAY);
myMinute = c.get(Calendar.MINUTE);
showDialog(ID_TIMEPICKER);
}
};
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case ID_DATEPICKER:
Toast.makeText(DateTimePicker.this,
"- onCreateDialog(ID_DATEPICKER) -",
Toast.LENGTH_LONG).show();
return new DatePickerDialog(this,
myDateSetListener,
myYear, myMonth, myDay);
case ID_TIMEPICKER:
Toast.makeText(DateTimePicker.this,
"- onCreateDialog(ID_TIMEPICKER) -",
Toast.LENGTH_LONG).show();
return new TimePickerDialog(this,
myTimeSetListener,
myHour, myMinute, false);
default:
return null;
}
}
private DatePickerDialog.OnDateSetListener myDateSetListener
= new DatePickerDialog.OnDateSetListener(){
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
String date = "Year: " + String.valueOf(year) + "\n"
+ "Month: " + String.valueOf(monthOfYear+1) + "\n"
+ "Day: " + String.valueOf(dayOfMonth);
Toast.makeText(DateTimePicker.this, date,
Toast.LENGTH_LONG).show();
}
};
private TimePickerDialog.OnTimeSetListener myTimeSetListener
= new TimePickerDialog.OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
String time = "Hour: " + String.valueOf(hourOfDay) + "\n"
+ "Minute: " + String.valueOf(minute);
Toast.makeText(DateTimePicker.this, time,
Toast.LENGTH_LONG).show();
}
};
}



Gambar 1 Date and Time Picker
2. Analog
Clock
Analog Untuk menampilkan jam
Analog. Kita hanya perlu membuatnya di file xml dan seperti biasa kemudian memanggilnya di kelas Activity.
analog.xml
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <AnalogClock android:id="@+id/clock1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> |
| package android.view; import android.app.Activity; import android.os.Bundle; public class AnalogClock extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.analog); } } |

Gambar
2 Analog
Clock
3. Digital
Clock
Untuk menampilkan jam
digital.
digital.xml
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <AnalogClock android:id="@+id/clock1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <DigitalClock android:id="@+id/clock2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> |

Gambar 2 Analog Clock
_netoya_
Posted at 08:15AM May 15, 2010 by nety ozora in General | Comments[0]