zoukankan      html  css  js  c++  java
  • DatePickerDialog和TimePickerDialog(基于对话框显示时间和日期)

    public class MainActivity extends Activity implements android.view.View.OnClickListener{
    private Button date_button;
    private Button time_button;
    private Calendar calendar;
    private TextView tv_date;
    private TextView tv_time;
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    date_button=(Button) findViewById(R.id.date_button);
    time_button=(Button) findViewById(R.id.time_button);
    tv_date=(TextView) findViewById(R.id.tv_date);
    tv_time=(TextView) findViewById(R.id.tv_time);
    date_button.setOnClickListener(this); //按下按钮显示日期对话框
    time_button.setOnClickListener(this); //按下按钮显示时间对话框
    //获取当前的年月日时分信息
    calendar=Calendar.getInstance();
    //显示日期对话框
    private void showDateDialog()
       {
    DatePickerDialog date_dialog=new DatePickerDialog(MainActivity.this, new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
    // TODO Auto-generated method stub
    tv_date.setText("现在日期:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
    }
    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

    date_dialog.show();
    }
    //显示时间对话框
    private void showTimeDialog()
    {
    TimePickerDialog time_dialog=new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // TODO Auto-generated method stub
    tv_time.setText("现在时间:"+hourOfDay+":"+minute);
    }
    }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
    time_dialog.show();
    }

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.date_button:
    showDateDialog();
    break;
    case R.id.time_button:
    showTimeDialog();
    break;
    }
  • 相关阅读:
    推理思维
    模糊数学
    Android 加法程序
    线程特点
    单例模式的优缺点
    lua 字符串过滤,特殊字符过滤
    cocos2dx
    cocos2dx之控制台输出
    C++基础(using)
    生活常识
  • 原文地址:https://www.cnblogs.com/cct1314520/p/6426683.html
Copyright © 2011-2022 走看看