我需要显示datePicker
选择EditText
点击事件的日期,我的EditText
位于AlertDialog
。我已为onClickListener
编写了代码,但未显示datePicker
。
public class TourActivity extends AppCompatActivity {
DatabaseHelper db;
private LayoutInflater inflater;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
db = new DatabaseHelper(getApplicationContext());
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = TourActivity.this.getLayoutInflater();
View content = inflater.inflate(R.layout.activity_add_new_trip, null);
final EditText editEvent = (EditText) content.findViewById(R.id.edTxt_EventName);
final EditText editStartDate = (EditText) content.findViewById(R.id.edTxt_EventSDate);
editStartDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(TourActivity.this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select date");
mDatePicker.show();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(TourActivity.this);
builder.setView(content)
.setTitle("Add Event")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Tour tour = new Tour();
tour.setEventId((int) System.currentTimeMillis());
tour.setEventName(editEvent.getText().toString().toUpperCase());
long t = db.createTour(tour);
Snackbar.make(findViewById(android.R.id.content),
"Event Added Successfully.", Snackbar.LENGTH_LONG)
.setActionTextColor(Color.RED)
.show();
displayList();
}
})
.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}public class TourActivity extends AppCompatActivity {
DatabaseHelper db;
private LayoutInflater inflater;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
db = new DatabaseHelper(getApplicationContext());
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = TourActivity.this.getLayoutInflater();
View content = inflater.inflate(R.layout.activity_add_new_trip, null);
final EditText editEvent = (EditText) content.findViewById(R.id.edTxt_EventName);
final EditText editStartDate = (EditText) content.findViewById(R.id.edTxt_EventSDate);
editStartDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(TourActivity.this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select date");
mDatePicker.show();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(TourActivity.this);
builder.setView(content)
.setTitle("Add Event")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Tour tour = new Tour();
tour.setEventId((int) System.currentTimeMillis());
tour.setEventName(editEvent.getText().toString().toUpperCase());
long t = db.createTour(tour);
Snackbar.make(findViewById(android.R.id.content),
"Event Added Successfully.", Snackbar.LENGTH_LONG)
.setActionTextColor(Color.RED)
.show();
displayList();
}
})
.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}
如果您需要任何其他代码,请告诉我。