我在下面附上了我的代码。我想在编辑文本的触摸事件事件上获得日期选择器的对话。我没有收到任何错误,但也没有收到任何日期对话。
请帮助我哪里错了。提前感谢。
String TAG = "AdvancedSearchActivity";
EditText txtArrCity, txtDepCity, txtDate, txtTime;
public String year, month, day;
public static final int DATE_DIALOG_ID = 1;
// OnDateSetListener dateListener;
String DATE;
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.advance_search);
txtArrCity = (EditText) findViewById(R.id.edittxt_ArrCity);
txtDepCity = (EditText) findViewById(R.id.edittxt_DepCity);
txtDate = (EditText) findViewById(R.id.edittxt_Date);
txtTime = (EditText) findViewById(R.id.edittxt_Time);
Calendar cal = Calendar.getInstance();
year = String.valueOf(cal.get(Calendar.YEAR));
month = String.valueOf(cal.get(Calendar.MONTH));
day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
Log.i(TAG, "Year" + year);
Log.i(TAG, "month" + (month + 1));
Log.i(TAG, "Day" + day);
updateDate();
txtDate.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
Log.i(TAG, "Inside On touch");
return false;
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(TabSample.tabcontext,
dateListener, Integer.parseInt(year),
Integer.parseInt(month), Integer.parseInt(day));
}
return null;
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int years, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
year = String.valueOf(years);
month = String.valueOf(monthOfYear);
day = String.valueOf(dayOfMonth);
updateDate();
}
};
private void updateDate() {
month = String.valueOf((Integer.parseInt(month) + 1));
if ((Integer.parseInt(month)) < 10) {
Log.i(TAG, "@@@@@ month" + month);
month = "0" + month;
Log.i(TAG, "@@@@@ AFTER month" + month + " MONTH S" + month);
}
day = String.valueOf(day);
if ((Integer.parseInt(day)) < 10) {
day = "0" + day;
}
DATE = year + "-" + month + "-" + day;
Log.i(TAG, "DATE : " + DATE);
txtDate.setText(DATE);
}