首先的思路就是想把datepincker和timepincker的内容显示到edt里,要先要有个方法来设置里面的东西
然后time就用setOnTimeChangedListener
而date没有这个方法要用init方法(设置年份,月份,天数,OnDateChangedListener());
最后在他们的点击事件里调用setdate方法就可以\
package com.example.datapinkertimepinker; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.EditText; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MainActivity extends Activity { private EditText show; private TimePicker time; private DatePicker date; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show = (EditText) findViewById(R.id.show); time = (TimePicker) findViewById(R.id.time); date = (DatePicker) findViewById(R.id.date); this.getDate(); time.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub MainActivity.this.getDate(); } }); date.init(date.getYear(), date.getMonth(), date.getDayOfMonth(), new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub MainActivity.this.getDate(); } }); } private void getDate() { show.setText(date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDayOfMonth() + " " + time.getCurrentHour() + ":" + time.getCurrentMinute()); } }
时间: 2024-12-06 13:21:31