android开发之DatePickerDialog与TimePickerDialog的使用

文章来源:http://www.itnose.net/detail/6249821.html

通过对话框的形式,来进行选择。尤其感觉DatePickerDialog比DatePicker在用户选择交互时好的多.

DatePickerDialog与TimePickerDialog都进行了很好的封装,对于开发者来说,直接调用,非常的简单。

android开发之DatePickerDialog与TimePickerDialog的使用  android开发之DatePickerDialog与TimePickerDialog的使用

演示实例:

public class MainActivity extends Activity {

	Button button,button2;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	public void dateClick(View v) {

		Calendar calendar = Calendar.getInstance();

		DatePickerDialog datePickerDialog = new DatePickerDialog(
				MainActivity.this, new OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker arg0, int year,
							int monthOfYear, int dayOfMonth) {
						
						//做你想做的事情

					}
				}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
		datePickerDialog.show();
		

	}
	
	public void timeClick(View v){
		
		Calendar calendar = Calendar.getInstance();
		TimePickerDialog dialog = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
			
			@Override
			public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
				//做你想做的事情
				
			}
		}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
		
		dialog.show();
	}
	
	
}
activity_main.xml
<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="dateClick"
        android:text="点击" />

    <Button
        android:id="@+id/but2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="timeClick"
        android:text="点击" />

</LinearLayout>


更多文章:http://www.itnose.net

相关推荐