activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ActivityMain" >
 
       <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        
	    <Button 
	    android:layout_width="wrap_content" 
	    android:layout_height="wrap_content"
	    android:text="@string/pick_time" 
	    android:id="@+id/btnTime"
	    android:paddingLeft="15dp"
	    android:paddingRight="15dp"
	    />
              
	    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtTime" 
        android:textSize="20sp"
        android:layout_marginLeft="20dp"
        />
	    
	    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="spinner"
        android:visibility="gone" />
	    
    </LinearLayout>
    
</RelativeLayout>

ActivityMain.java sınıfı

package tr.com.emrahkahraman.timepicker;

import java.util.Calendar;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class ActivityMain extends ActionBarActivity {

	private TextView txtZaman = null;
	private TimePicker timePicker;
	private Button btnZaman;

	private int hour;
	private int minute;
	
	static final int TIME_DIALOG_ID = 999;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		txtZaman = (TextView) findViewById(R.id.txtTime);
	    timePicker = (TimePicker) findViewById(R.id.timePicker);
	    btnZaman = (Button) findViewById(R.id.btnTime);
	    
		final Calendar c = Calendar.getInstance();
		hour = c.get(Calendar.HOUR_OF_DAY);
		minute = c.get(Calendar.MINUTE);
		txtZaman.setText(new StringBuilder().append(padding_str(hour)).append(":").append(padding_str(minute)));

		timePicker.setCurrentHour(hour);
		timePicker.setCurrentMinute(minute);
		btnZaman.setOnClickListener(clickListener);
	}

	private OnClickListener clickListener = new OnClickListener() 
	   {
		    public void onClick(View v) 
		    {   
		    	switch (v.getId())
		    	{
		    	case R.id.btnTime:
		    		showDialog(TIME_DIALOG_ID);
		    		break;
		    	}
		   }
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, timePickerListener, hour, minute,false);
		}
		return null;
	}

	private TimePickerDialog.OnTimeSetListener timePickerListener =  new TimePickerDialog.OnTimeSetListener() 
       {
		public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
			hour = selectedHour;
			minute = selectedMinute;
			txtZaman.setText(new            StringBuilder().append(padding_str(hour)).append(":").append(padding_str(minute)));
			timePicker.setCurrentHour(hour);
			timePicker.setCurrentMinute(minute);
		}
	};
	
	private static String padding_str(int c) 
	{
		if (c >= 10)
		   return String.valueOf(c);
		else
		   return "0" + String.valueOf(c);
	}
}

Ekran Görüntüsü
Screenshot_2015-05-10-15-42-18[1]
Proje Kodunu İndirmek İçin Tıklayınız

Etiketler: ,

Yazar : Emrah Kahraman

Bilgisayar Mühendisi

Android TimePicker Kullanımı Yazısı için Yorum Yapabilirsiniz

Sponsor
Alexa
Hakkımda
Bağlantılar