为了账号安全,请及时绑定邮箱和手机立即绑定

OnDateChangedListener中onDateChanged方法内打印日志为什么都是两次?

datePicker.init(year, cal.get(Calendar.MONTH), day, new DatePicker.OnDateChangedListener() {
   @Override
   public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
       Log.i("tag", "======" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
   }
});

这边每一次监听到,打印日志都是两次

正在回答

2 回答

全部代码如下:

只有onDateChanged中的日志会打印两遍

public class DateTimePickerActivity extends Activity {

   private DatePicker datePicker;
   private TimePicker timePicker;
   private Calendar cal;
   private int year,month,day,hour, minute;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.test_date_time_picker);

       datePicker = (DatePicker) findViewById(R.id.datePicker);
       timePicker = (TimePicker) findViewById(R.id.timePicker);
       cal = Calendar.getInstance();
       year = cal.get(Calendar.YEAR);
       month = cal.get(Calendar.MONTH)+1;
       day = cal.get(Calendar.DAY_OF_MONTH);
       hour = cal.get(Calendar.HOUR_OF_DAY);
       minute = cal.get(Calendar.MINUTE);

       Log.i("tag", year + "-" + month + "-" + day + "-" + hour + ":" + minute);

       datePicker.init(year, cal.get(Calendar.MONTH), day, new DatePicker.OnDateChangedListener() {
           @Override
           public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
               Log.i("tag", "======" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
           }
       });

       timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
           @Override
           public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
               Log.i("tag", "------" + hourOfDay + ":" + minute);
           }
       });

       /**
        * @param context The context the dialog is to run in.
        * @param callBack How the parent is notified that the date is set.
        * @param year The initial year of the dialog.
        * @param monthOfYear The initial month of the dialog.
        * @param dayOfMonth The initial day of the dialog.
        */
//        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
//            @Override
//            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//                Log.i("tag", "*******" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
//            }
//        }, year, cal.get(Calendar.MONTH), day).show();


       /**
        * @param context Parent.
        * @param callBack How parent is notified.
        * @param hourOfDay The initial hour.
        * @param minute The initial minute.
        * @param is24HourView Whether this is a 24 hour view, or AM/PM.
        */
       new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
           @Override
           public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
               Log.i("tag", "........" + hourOfDay + "-" + minute);
           }
       }, hour, minute, true).show();
   }
}

0 回复 有任何疑惑可以回复我~

你看看是不是哪里也调用了这个方法

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android攻城狮的第二门课(第1季)
  • 参与学习       111172    人
  • 解答问题       1457    个

本课程由浅入深地带您学会Android的常用控件的开发和使用

进入课程

OnDateChangedListener中onDateChanged方法内打印日志为什么都是两次?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信