package com.example.myact5; import java.util.Calendar; import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
/**
*
* @Describe:
* @package: com.example.myact5
* @author shaobn
* @date 2015-9-11 上午9:10:28
*/
public class MainActivity extends ActionBarActivity {
private Button button;
private Button button2;
private Calendar calendar = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TimePickerDialog tpd = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() { @Override
public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,getConstantInstance().hourOfDay+","+ getConstantInstance().minute, 1).show();
}
}, getConstantInstance().hourOfDay, getConstantInstance().minute, true);
tpd.show();
}
});
button2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, new OnDateSetListener() { @Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, arg1+","+(arg2+1)+","+arg3, 1).show();
}
}, getConstantInstance().year, getConstantInstance().monthOfYear, getConstantInstance().dayOfMonth);
dpd.show();
}
});
}
class Constant{
public int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
public int minute = calendar.get(Calendar.MINUTE);
public boolean is24HourView = true;
public int year = calendar.get(Calendar.YEAR);
public int monthOfYear = calendar.get(Calendar.MONTH);
public int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
}
public Constant getConstantInstance(){
return new Constant();
}
}
笔者写的麻烦点,为了练习一下。