我想在Android片段中显示日期选择器对话框。我找到了一些例子,但是我没有正确地得到它。我有两个编辑文本(分别称为 R.id.event_start_time_a 和 R.id.event_end_time_a ),当我单击该文本时,应该会弹出日期选择器对话框,并在设置完日期,则日期应以dd / mm / yyyy格式显示在编辑文本中。请提供任何帮助。
完整代码。
编辑:添加事件片段:
public class AddEventFragment extends Fragment {
private static final String TAG = "AddEventFragment";
public final Calendar myCalendar = Calendar.getInstance();
private FirebaseFirestore firestoreDB;
private boolean isEdit;
private String docId;
public AddEventFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_event, container, false);
Button button = (Button) view.findViewById(R.id.add_event);
Event event = null;
if(getArguments() != null){
event = getArguments().getParcelable("event");
((TextView)view.findViewById(R.id.add_tv)).setText("Edit Event");
}
if(event != null){
((TextView) view.findViewById(R.id.event_name_a)).setText(event.getName());
((TextView) view.findViewById(R.id.event_type_a)).setText(event.getType());
((TextView) view.findViewById(R.id.event_place_a)).setText(event.getPlace());
final EditText startTime = (EditText)view.findViewById(R.id.event_start_time_a);
startTime.setText(event.getStartTime());
startTime.setFocusable(false);
startTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
startTime.setText(sdf.format(myCalendar.getTime()));
}
});
final EditText endTime = (EditText)view.findViewById(R.id.event_end_time_a);
endTime.setText(event.getEndTime());
endTime.setFocusable(false);
endTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
endTime.setText(sdf.format(myCalendar.getTime()));
}
});
button.setText("Edit Event");
isEdit = true;
docId = event.getId();
}
firestoreDB = FirebaseFirestore.getInstance();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(!isEdit){
addEvent();
}else {
updateEvent();
}
}
});
return view;
}
final DatePickerDialog.OnDateSetListener mdate = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, month);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
};
public void showDatePickerDialog(){
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), mdate, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
public void addEvent(){
Event event = createEventObj();
addDocumentToCollection(event);
}
public void updateEvent(){
Event event = createEventObj();
updateDocumentToCollection(event);
}
private Event createEventObj(){
final Event event = new Event();
event.setName(((TextView)getActivity().findViewById(R.id.event_name_a)).getText().toString());
event.setPlace(((TextView)getActivity().findViewById(R.id.event_place_a)).getText().toString());
event.setType(((TextView)getActivity().findViewById(R.id.event_type_a)).getText().toString());
final EditText startTime1 = (EditText)getActivity().findViewById(R.id.event_start_time_a);
startTime1.setText(event.getStartTime());
startTime1.setFocusable(false);
startTime1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
startTime1.setText(sdf.format(myCalendar.getTime()));
}
});
final EditText endTime1 = (EditText)getActivity().findViewById(R.id.event_start_time_a);
endTime1.setText(event.getEndTime());
endTime1.setFocusable(false);
endTime1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
endTime1.setText(sdf.format(myCalendar.getTime()));
}
});
//event.setStartTime(((TextView)getActivity().findViewById(R.id.event_start_time_a)).getText().toString());
//event.setEndTime(((TextView)getActivity().findViewById(R.id.event_end_time_a)).getText().toString());
return event;
}
private void addDocumentToCollection(Event event){
firestoreDB.collection("events")
.add(event)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d(TAG, "Event document added - id: " + documentReference.getId());
restUi();
Toast.makeText(getActivity(), "Event document has been added", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding event document", e);
Toast.makeText(getActivity(), "Event document could not be added", Toast.LENGTH_SHORT).show();
}
});
}
private void updateDocumentToCollection(Event event){
firestoreDB.collection("events")
.document(docId)
.set(event, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Event document updated ");
Toast.makeText(getActivity(),"Event document has been updated", Toast.LENGTH_SHORT).show();
showEventScreen();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding event document", e);
Toast.makeText(getActivity(), "Event document could not be added", Toast.LENGTH_SHORT).show();
}
});
}
private void restUi(){
((TextView)getActivity().findViewById(R.id.event_name_a)).setText("");
((TextView)getActivity().findViewById(R.id.event_type_a)).setText("");
((TextView)getActivity().findViewById(R.id.event_place_a)).setText("");
((TextView)getActivity().findViewById(R.id.event_start_time_a)).setText("");
((TextView)getActivity().findViewById(R.id.event_end_time_a)).setText("");
}
private void showEventScreen() {
Intent i = new Intent();
i.setClass(getActivity(), EventActivity.class);
startActivity(i);
}
}
答案 0 :(得分:2)
您没有在任何地方致电showDatePickerDialog()
。您需要将onClickListener()
添加到textview
中,然后在其中调用它:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//...
EditText startTime = view.findViewById(R.id.event_start_time_a));
startTime.setText(event.getStartTime());
startTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDatePickerDialog();
}
});
EditText endTime = view.findViewById(R.id.event_end_time_a));
endTime.setText(event.getEndTime());
endTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDatePickerDialog();
}
});
//...
}
}
答案 1 :(得分:0)
int mYear, mMonth, mDay;
Calendar c;
private void show_Datepicker(){
c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(context,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int month = monthOfYear + 1; // month count given less one e.g. august then give month no 7
String dayStr = String.valueOf(dayOfMonth);
String monthStr = String.valueOf(month);
if (dayStr.length() == 1)
dayStr = "0"+dayStr;
if (monthStr.length() == 1)
monthStr = "0"+monthStr;
// dd/mm/yyyy format set.
your_edit_text.setText(dayStr+"/"+monthStr+"/"+year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}