我在我的应用程序中使用DatePickerDialog
。这是碎片。现在,问题是,当我在EditText
上休息时,它打开对话3次。
我在这里为2个不同的DatePickerDialog
和2个EditText
使用了2个TimePickerDialog
的总计2 EditText
。
我的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((EditText) etIOStartDate).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
new DatePickerDialog(getActivity(), StartDate, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
return false;
}
});
}
DatePickerDialog.OnDateSetListener StartDate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat,
Locale.getDefault());
((EditText) etIOStartDate)
.setText(sdf.format(myCalendar.getTime()));
}
};
请帮我解决问题。
答案 0 :(得分:3)
试试这个。有两个事件Action down和Action up。
((EditText) etIOStartDate).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
switch(arg1.getAction())
{
case MotionEvent.ACTION_DOWN :
new DatePickerDialog(getActivity(), StartDate, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
break;
case MotionEvent.ACTION_UP :
break;
}
return true;
}
});
答案 1 :(得分:1)
将setOnClickListener改为EditText
你已经使用了setOnTouchListener,所以它会发现2-3次触摸,所以它会打开
答案 2 :(得分:1)
你必须维护一个全局变量来检查你的datepicker是否被打开。当您打开datepicker时,将该变量赋值为true。
boolean isDatePickerOpened=false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((EditText) etIOStartDate).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(!isDatePickerOpened){
isDatePickerOpened=true;
new DatePickerDialog(getActivity(), StartDate, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
return false;
}
});
}
DatePickerDialog.OnDateSetListener StartDate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat,
Locale.getDefault());
((EditText) etIOStartDate)
.setText(sdf.format(myCalendar.getTime()));
isDatePickerOpened=false;
}
};
当日期选择器对话框被解除时,您必须将isDatePickerOpened设置为false。这可能对你有帮助..
答案 3 :(得分:0)
在onCreate()
下使用此功能txtDate = (EditText) findViewById(R.id.etSdate);
txtDate.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
etSdate();
}
});
在程序结束时调用此函数
public void etSdate()
{
{
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
}