1
public class QuickIWantActivity extends Activity implements
        OnDateSetListener {
.....
.....
.....

dateET.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

Calendar cal = Calendar.getInstance();
DatePickerDialog datePickDiag = new DatePickerDialog(
        QuickIWantActivity.this, QuickIWantActivity.this, cal
            .get(Calendar.YEAR), cal.get(Calendar.MONTH),
        cal.get(Calendar.DAY_OF_MONTH));
        datePickDiag.show();
        return true;
        }
    });

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {

    int month = monthOfYear + 1;
    dateET.setText(dayOfMonth + "- " + month + "- " + year);
}

}

因此,在触摸作为 EditText 的 dateET 时,它会打开日期选择器,我设置新日期并单击设置,它不会被解除,我再次单击设置,即使现在它也不会被解除,最后当我单击设置时它会被解除第三次

4

1 回答 1

3

onTouch接收三个事件(正常序列):

MotionEvent.ACTION_DOWN
MotionEvent.ACTION_MOVE
MotionEvent.ACTION_UP

并且由于您没有检查其中任何一个(并返回 true),因此您的代码将执行 3 次(每个事件执行一次)。

解决方案:

使用onClick()(这是我会做的)或添加检查:

@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) { 
  case MotionEvent.ACTION_DOWN:

Calendar cal = Calendar.getInstance();
DatePickerDialog datePickDiag = new DatePickerDialog(
        QuickIWantActivity.this, QuickIWantActivity.this, cal
            .get(Calendar.YEAR), cal.get(Calendar.MONTH),
        cal.get(Calendar.DAY_OF_MONTH));
        datePickDiag.show();
        return false;
        }
    });
break;
}
于 2012-12-12T13:38:54.797 回答