DatePicker是android的一个日期显示,它的框架已经存在的,如图:
它是不需要输入的,只是需要输入开始提示的默认值。
package com.example.acti_02_02_datedialog;
import android.os.Bundle;
import android.R.integer;
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 showDatePickerButton = null;
private static final int DATE_PICKER_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDatePickerButton = (Button)findViewById(R.id.showDatePickerButton);
showDatePickerButton.setOnClickListener(new ButtonListener());
}
private class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_PICKER_ID);
}
}
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 + "-" + monthOfYear + "-" + dayOfMonth );
}
};
protected Dialog onCreateDialog(int id){
switch(id){
case DATE_PICKER_ID:
return new DatePickerDialog(this, onDateSetListener, 2010, 11, 25);
}
return null;
}
@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;
}
}