package com.example.testdatedialog;
/*
* 时间显示 datepicker,使用
* */
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
public class MainActivity extends Activity {
private Button button = null;
//创建一个常量,用来标识DatePickerDialog
private static final int DATE_PICKER_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonOnClickListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/*
* 使用内部匿名类,实现DatePickerDialog.OnDateSetListener接口,重写onDateSet()方法
* 点击set按钮 确定
* */
DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
System.out.println("year----"+year);
System.out.println("Month---"+(monthOfYear+1));//月是从0开始计数的 所以加1
System.out.println("day-----"+dayOfMonth);
}
};
/*重写创建dialog的函数,根据id来创建dialog
* 当调用showdialog()方法的时候就会执行这个函数
*/
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case DATE_PICKER_ID:
return new DatePickerDialog(this, onDateSetListener, 2013, 11, 25);
default:
break;
}
return null;
}
//button的监听器
private class ButtonOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_PICKER_ID);
}
}
}
/*
* 时间显示 datepicker,使用
* */
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
public class MainActivity extends Activity {
private Button button = null;
//创建一个常量,用来标识DatePickerDialog
private static final int DATE_PICKER_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonOnClickListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/*
* 使用内部匿名类,实现DatePickerDialog.OnDateSetListener接口,重写onDateSet()方法
* 点击set按钮 确定
* */
DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
System.out.println("year----"+year);
System.out.println("Month---"+(monthOfYear+1));//月是从0开始计数的 所以加1
System.out.println("day-----"+dayOfMonth);
}
};
/*重写创建dialog的函数,根据id来创建dialog
* 当调用showdialog()方法的时候就会执行这个函数
*/
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case DATE_PICKER_ID:
return new DatePickerDialog(this, onDateSetListener, 2013, 11, 25);
default:
break;
}
return null;
}
//button的监听器
private class ButtonOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_PICKER_ID);
}
}
}