{"id":1515,"date":"2019-02-27T16:20:28","date_gmt":"2019-02-27T16:20:28","guid":{"rendered":"https:\/\/codewk.com\/?p=1515"},"modified":"2019-04-05T11:41:35","modified_gmt":"2019-04-05T11:41:35","slug":"android-date-time-picker-kullanimi","status":"publish","type":"post","link":"https:\/\/codewk.com\/android-date-time-picker-kullanimi\/","title":{"rendered":"Android – Date Picker & Time Picker Kullan\u0131m\u0131"},"content":{"rendered":"\n
Merhaba arkada\u015flar, bu yaz\u0131da Android uygulamalarda kullan\u0131lan Date Picker ve Time Picker nedir, nas\u0131l kullan\u0131l\u0131r bunlar\u0131 \u00f6\u011frenece\u011fiz ve bununla ilgili Button ve TextView’lerden olu\u015fan bir uygulama yapaca\u011f\u0131z. Gerek web uygulamalarda, gerekse mobil uygulamalarda kullan\u0131lan zaman kavram\u0131 \u00e7ok \u00f6nemlidir. \u015eimdir ise Date Picker ve Time Picker’\u0131n ne oldu\u011funu anlamaya \u00e7al\u0131\u015fal\u0131m. Ve daha sonras\u0131nda da basit bir uygulama ile olay\u0131 kavramaya \u00e7al\u0131\u015fal\u0131m. Ba\u015fl\u0131yoruz.. ????<\/p>\n\n\n\n
Date Picker :<\/strong> Anlam\u0131 “tarih se\u00e7ici<\/em>“dir. Tarih belirlemede kullan\u0131l\u0131r. Bu uygulamada giri\u015f ekran\u0131nda “Saat” ve “Tarih” ad\u0131nda iki buton olacak ve kullan\u0131c\u0131lar a\u00e7\u0131lan diyalog penceresinden saat ve tarihi se\u00e7ebilecekler ve biz de bu iki yap\u0131n\u0131n kullan\u0131m\u0131n\u0131 anlam\u0131\u015f olaca\u011f\u0131z. Hadi ba\u015flayal\u0131m !<\/p>\n\n\n\n Uygulaman\u0131n tasar\u0131m\u0131n\u0131 istedi\u011finiz gibi d\u00fczenleyebilirsiniz. \u00d6nemli olan se\u00e7ilen de\u011ferlerin g\u00f6sterilebilece\u011fi bir TextView olmas\u0131d\u0131r. Tabi farkl\u0131 fikirler de olabilir.<\/p>\n\n\n\n Kodlar\u0131 k\u0131saca a\u00e7\u0131klayay\u0131m. Ana s\u0131n\u0131fta \u00f6nemli olan yap\u0131lar TimePickerDialog, DatePickerDialog ve Calendar. Bu s\u0131n\u0131flardan nesne \u00fcretmekle ba\u015fl\u0131yoruz. Butonlara t\u0131kland\u0131\u011f\u0131nda tarih ve saat diyalog pencerelerinin \u00e7\u0131kmas\u0131n\u0131 sa\u011flayan kodlar\u0131, butonlar\u0131n onClick() metodu i\u00e7erisine yaz\u0131yoruz. Integer cinsinden tan\u0131mlad\u0131\u011f\u0131m\u0131z hour<\/em>, minute<\/em>, day<\/em>, month<\/em> ve year<\/em>‘a ise se\u00e7ilen zamanlar atan\u0131r. \u00d6rnek \u27a1 <\/p>\n\n\n\n
Time Picker :<\/strong> Anlam\u0131 “saat se\u00e7ici<\/em>“dir. Saat belirlemede kullan\u0131l\u0131r.
Bu iki yap\u0131 kullan\u0131c\u0131lar\u0131n manuel olarak saat ve tarih belirlemesine gerek kalmadan basit bir diyalog penceresi kullanmas\u0131n\u0131 sa\u011flar.<\/p>\n\n\n\n\u00d6rnek Uygulama :<\/h2><\/strong><\/p>\n\n\n\n
activity_main.xml<\/h2><\/font>\n\n\n\n
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"
xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"
xmlns:tools=\"http:\/\/schemas.android.com\/tools\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:background=\"#e0e0e0\"
tools:context=\".MainActivity\"
tools:layout_editor_absoluteY=\"25dp\">
<Button
android:id=\"@+id\/button\"
android:layout_width=\"115dp\"
android:layout_height=\"60dp\"
android:layout_marginEnd=\"20dp\"
android:layout_marginLeft=\"68dp\"
android:layout_marginRight=\"20dp\"
android:layout_marginStart=\"68dp\"
android:background=\"#463c48\"
android:text=\"SAAT\"
app:layout_constraintBaseline_toBaselineOf=\"@+id\/button2\"
app:layout_constraintEnd_toStartOf=\"@+id\/button2\"
app:layout_constraintStart_toStartOf=\"parent\" \/>
<Button
android:id=\"@+id\/button2\"
android:layout_width=\"115dp\"
android:layout_height=\"60dp\"
android:layout_marginBottom=\"71dp\"
android:layout_marginEnd=\"68dp\"
android:layout_marginRight=\"68dp\"
android:background=\"#463c48\"
android:text=\"TAR\u0130H\"
app:layout_constraintBottom_toTopOf=\"@+id\/textView2\"
app:layout_constraintEnd_toEndOf=\"parent\"
app:layout_constraintStart_toEndOf=\"@+id\/button\"
app:layout_constraintTop_toBottomOf=\"@+id\/textView\" \/>
<TextView
android:id=\"@+id\/textView\"
android:layout_width=\"wrap_content\"
android:layout_height=\"0dp\"
android:layout_marginBottom=\"75dp\"
android:layout_marginLeft=\"8dp\"
android:layout_marginStart=\"8dp\"
android:layout_marginTop=\"128dp\"
android:text=\" Tarih ve saati se\u00e7mek i\u00e7in butonlara t\u0131klay\u0131n\u0131z.\"
android:textColor=\"#000000\"
android:textSize=\"18dp\"
app:layout_constraintBottom_toTopOf=\"@+id\/button2\"
app:layout_constraintStart_toStartOf=\"parent\"
app:layout_constraintTop_toTopOf=\"parent\" \/>
<TextView
android:id=\"@+id\/textView2\"
android:layout_width=\"wrap_content\"
android:layout_height=\"30dp\"
android:layout_marginBottom=\"23dp\"
android:paddingLeft=\"10dp\"
android:textColor=\"#000000\"
android:textSize=\"20dp\"
android:textStyle=\"bold\"
app:layout_constraintBottom_toTopOf=\"@+id\/textView3\"
app:layout_constraintEnd_toEndOf=\"parent\"
app:layout_constraintStart_toStartOf=\"parent\"
app:layout_constraintTop_toBottomOf=\"@+id\/button2\" \/>
<TextView
android:id=\"@+id\/textView3\"
android:layout_width=\"wrap_content\"
android:layout_height=\"30dp\"
android:layout_marginBottom=\"92dp\"
android:paddingRight=\"10dp\"
android:textColor=\"#000000\"
android:textSize=\"20dp\"
android:textStyle=\"bold\"
app:layout_constraintBottom_toBottomOf=\"parent\"
app:layout_constraintEnd_toEndOf=\"parent\"
app:layout_constraintStart_toStartOf=\"parent\"
app:layout_constraintTop_toBottomOf=\"@+id\/textView2\" \/>
<\/android.support.constraint.ConstraintLayout><\/pre>\n\n\n\n
<\/figcaption><\/figure>\n\n\n\nMainActivity.java<\/h2><\/font>\n\n\n\n
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView2,textView3;
Calendar calendar;
DatePickerDialog datePickerDialog;
int hour,minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main<\/em>);
button = (Button) findViewById(R.id.button<\/em>);
button2 = (Button) findViewById(R.id.button2<\/em>);
textView2 = (TextView) findViewById(R.id.textView2<\/em>);
textView3 = (TextView) findViewById(R.id.textView3<\/em>);
calendar = Calendar.getInstance<\/em>();
hour = calendar.get(Calendar.HOUR_OF_DAY<\/em>);
minute = calendar.get(Calendar.MINUTE<\/em>);
\/\/textView2.setText(hour + \" : \" + minute);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
textView2.setText(hourOfDay + \" : \"+ minute );
}
}, hour,minute,true);
timePickerDialog.show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendar = Calendar.getInstance<\/em>();
int day = calendar.get(Calendar.DAY_OF_MONTH<\/em>);
int month = calendar.get(Calendar.MONTH<\/em>);
int year = calendar.get(Calendar.YEAR<\/em>);
datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
textView3.setText(dayOfMonth + \"\/\" + (month+1) + \"\/\" + year);
}
}, day,month,year);
datePickerDialog.show();
}
});
}
}
<\/pre>\n\n\n\nhour = calendar.get(Calendar.HOUR_OF_DAY<\/em>);
minute = calendar.get(Calendar.MINUTE<\/em>);
<\/pre>\n\n\n\n