I've implemented an android screen with an Edittext. When the user touches this EditText, a datepickerdialog appears. This all worked fine, however my problem is that when the user touches the EditText, two datepickerdialogs are appearing. I only want one to appear.
I' wondering if anyone has encountered this problem, or is it only me? If yes, are there any solutions? I Google'd it, however found nothing :/
With regards to code, this is what I have:
datebox = (TextView)findViewById(R.id.datebox);
datebox.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
timedatedialog(false);
return false;
}});
public void timedatedialog(boolean flag) {
if(flag==false){
DatePickerDialog dateDlg = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
Time chosenDate = new Time();
chosenDate.set(dayOfMonth, monthOfYear, year);
long dtDob = chosenDate.toMillis(true);
CharSequence strDate = DateFormat.format("MMMM dd, yyyy", dtDob);
Toast.makeText(ForecastingActivity.this,
"Date picked: " + strDate, Toast.LENGTH_SHORT).show();
datebox.setText(strDate);
}}, 2012,0, 1);
Any help is much appreciated. Thanks!
Change your
OnTouchListener
to anOnClickListener
or similiar. TheonTouch()
-method is invoked for any kind of touch event, i.e:if you click the view you will go into
onTouch()
with a down-event,and then if you happen move your finger the slightest you will go into
onTouch()
with a move-action, and finally you lift your finger and getonTouch()
again, this time with a up-event.The
OnClickListener
on the other hand only runs once, if you click the view.