日期选择器对话框显示多次 [英] Date picker dialog shows multiple times

查看:50
本文介绍了日期选择器对话框显示多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要从用户那里获取日期的android活动.我的活动有一个编辑文本,当它被触摸时会显示datepicker对话框并在编辑文本中显示日期.我已经使用Datepickerdialog完成了它.是当我单击edittext 2 datepickerdialog时显示的.

I am creating an android activity which needs to get date from user.My activity has a edit text when it is touched shows the datepicker dialog and shows the date in the edit text.I have completed it by using Datepickerdialog.My problem is when i click the edittext 2 datepickerdialog are shown.

这是我的代码...

Activity.class

Activity.class

EditText startDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_event);
    startDate= (EditText) findViewById(R.id.startdate);
    startDate.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            new DatePickerDialogFragment().show(getFragmentManager(),"DatePicker");

            return true;
        }
    });
}

@Override
public void returnDate(String date) {
    startDate.setText(date);
}

DatepickerDialog类

DatepickerDialog class

public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

TheListener listener;

public interface TheListener{
    public void returnDate(String date);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar calendar=Calendar.getInstance();
    int year=calendar.get(Calendar.YEAR);
    int month=calendar.get(Calendar.MONTH);
    int day=calendar.get(Calendar.DAY_OF_MONTH);
    listener = (TheListener) getActivity();


    return new DatePickerDialog(getActivity(),this,year,month,day);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Calendar c=Calendar.getInstance();
    c.set(year,monthOfYear,dayOfMonth);

    SimpleDateFormat dateFormat=new SimpleDateFormat("dd-MM-yyyy");
    String date=dateFormat.format(c.getTime());
    if(listener!=null)
    {
        listener.returnDate(date);
    }

}
}

我的代码中有什么问题以及如何解决.

what is the problem in my code.and how to solve it.

推荐答案

使用 ACTION_UP 运动事件像这样编写您的代码

write your code like this with ACTION_UP motion event

 @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    new DatePickerDialogFragment().show(getFragmentManager(),"DatePicker");                      
                }
                return false;
            }

这篇关于日期选择器对话框显示多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆