如何为 Android 片段内的文本视图显示或设置日期选择器对话框?
How to show or set datepicker dialog for a textview that is inside a fragment in Android?
我绝对是 Android 的初学者。我正在尝试使用日期选择器对话框为片段内的文本字段设置文本。
这是 activity
中的文本字段片段
public class CreateTaskFragment extends Fragment{
private Button saveBtn;
private EditText tfDescription,tfDate;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.create_task,container,false);
tfDescription = (EditText)view.findViewById(R.id.tf_task_description);
tfDate = (EditText)view.findViewById(R.id.tf_task_date);
tfDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment pickerFragment = new SelectDateFragment();
pickerFragment.show(getFragmentManager(),"DatePicker");
}
});
saveBtn = (Button)view.findViewById(R.id.btn_save_task);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveTask();
}
});
return view;
}
public void saveTask()
{
String description = tfDescription.getText().toString();
String date = tfDate.getText().toString();
}
}
这是选择器的对话框片段
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm+1, dd);
}
public void populateSetDate(int year, int month, int day) {
//set the date here
}
}
我的问题是如何为上一个片段的文本字段设置选定日期。 Datepicker 正在工作,我不知道如何为编辑文本设置选定日期。
您可以将日期设置到描述的EditText。如:
@Override
public void onDateSet(DatePicker dialog, int year, int monthOfYear, int dayOfMonth) {
//set the text of description
tfDescription.setText("Year:" + year + " Month: " + monthOfYear + "Day: " + dayOfMonth);
//get the data of date by using Calendar
Calendar calendar = Calendar.getInstance();
calendar.set(year,monthOfYear,dayOfMonth);
mCurrentDate = calendar.getTimeInMillis();
}
表示可以访问@Override public void onDateSet
中Date的数据
为将获取 EditText 引用的 SelectDateFragment 创建构造函数,并在 onDate() 中将日期设置为该引用。就这么简单。
给你一个简单的解决方案,把OnClick里面的代码换成这个
public void onClick(View v) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date = "Date"+String.valueOf(year) +"-"+String.valueOf(monthOfYear)
+"-"+String.valueOf(dayOfMonth);
tfDescription.setText(date);
tfDate.setText(date);
}
}, yy, mm, dd);
datePicker.show();
您会看到 tfDescription/tfDate 的 EditText 在您选择日期后发生了变化
我绝对是 Android 的初学者。我正在尝试使用日期选择器对话框为片段内的文本字段设置文本。
这是 activity
中的文本字段片段public class CreateTaskFragment extends Fragment{
private Button saveBtn;
private EditText tfDescription,tfDate;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.create_task,container,false);
tfDescription = (EditText)view.findViewById(R.id.tf_task_description);
tfDate = (EditText)view.findViewById(R.id.tf_task_date);
tfDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment pickerFragment = new SelectDateFragment();
pickerFragment.show(getFragmentManager(),"DatePicker");
}
});
saveBtn = (Button)view.findViewById(R.id.btn_save_task);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveTask();
}
});
return view;
}
public void saveTask()
{
String description = tfDescription.getText().toString();
String date = tfDate.getText().toString();
}
}
这是选择器的对话框片段
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm+1, dd);
}
public void populateSetDate(int year, int month, int day) {
//set the date here
}
}
我的问题是如何为上一个片段的文本字段设置选定日期。 Datepicker 正在工作,我不知道如何为编辑文本设置选定日期。
您可以将日期设置到描述的EditText。如:
@Override
public void onDateSet(DatePicker dialog, int year, int monthOfYear, int dayOfMonth) {
//set the text of description
tfDescription.setText("Year:" + year + " Month: " + monthOfYear + "Day: " + dayOfMonth);
//get the data of date by using Calendar
Calendar calendar = Calendar.getInstance();
calendar.set(year,monthOfYear,dayOfMonth);
mCurrentDate = calendar.getTimeInMillis();
}
表示可以访问@Override public void onDateSet
为将获取 EditText 引用的 SelectDateFragment 创建构造函数,并在 onDate() 中将日期设置为该引用。就这么简单。
给你一个简单的解决方案,把OnClick里面的代码换成这个
public void onClick(View v) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date = "Date"+String.valueOf(year) +"-"+String.valueOf(monthOfYear)
+"-"+String.valueOf(dayOfMonth);
tfDescription.setText(date);
tfDate.setText(date);
}
}, yy, mm, dd);
datePicker.show();
您会看到 tfDescription/tfDate 的 EditText 在您选择日期后发生了变化