Android DatePickerDialog和TimePickerDialog

package com.example.androidtest;


import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.widget.*;


import java.util.Calendar;


public class MainActivity extends Activity {


private Button dateButton=null;
private Button timeButton=null;
private TextView dateTextView=null;
private TextView timeTextView=null;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateButton=(Button)findViewById(R.id.dateButton);
timeButton=(Button)findViewById(R.id.timeButton);
dateTextView=(TextView)findViewById(R.id.dateTextView);
timeTextView=(TextView)findViewById(R.id.timeTextView);
/*获取当前日期*/
Calendar calendar=Calendar.getInstance();
mYear=calendar.get(Calendar.YEAR);
mMonth=calendar.get(Calendar.MONTH);
mDay=calendar.get(Calendar.DAY_OF_MONTH);
mHour=calendar.get(Calendar.HOUR_OF_DAY);
mMinute=calendar.get(Calendar.MINUTE);
/*选择日期*/
dateButton.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View arg0) {
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
dateTextView.setText(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
}
}, mYear, mMonth, mDay).show();
}
});
/*选择时间*/
timeButton.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeTextView.setText(hourOfDay+"时"+minute+"分");
}
}, mHour, mMinute, true).show();
}
});
}

@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;
}


}


你可能感兴趣的:(android)