我有一项活动打开DialogFragment
,其中包含DatePicker
。如何将选定日期返回到我的活动及其在TextView
中的状态?我检查了Android文档中的示例,但它们根本不清楚。我是编程新手。我找到的所有教程都只显示如何在DialogFragment
内的吐司中显示日期。
这是我的DialogFrament代码
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
DatePickerFragmentListener datePickerListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
//this makes current date as the default date of the calander
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public interface DatePickerFragmentListener {
void onDateSet(DatePicker view, int year, int month, int day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day ){
// what do i put here?
}
}
我在活动中将onDateSet方法放在什么内?
public class FilterGuideList extends AppCompatActivity implements DatePickerFragment.DatePickerFragmentListener {
private LinearLayout from_date;
private LinearLayout to_date;
private TextView from_text;
private TextView to_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter_guide_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//initializing linearlayouts for date picker clickevent
from_date = (LinearLayout) findViewById(R.id.from_date_selector);
to_date = (LinearLayout) findViewById(R.id.to_date_selector);
from_text = (TextView) findViewById(R.id.from_date_text);
to_text = (TextView) findViewById(R.id.to_date_text);
from_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
});
to_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// what do i put here?
}
}
我一直试图让这个工作2天了。任何帮助将非常感激。
答案 0 :(得分:0)
https://github.com/wdullaer/MaterialDateTimePicker 将以上库用于datePicker。
dependencies {
compile 'com.wdullaer:materialdatetimepicker:2.3.0'
}
实施OnDateSetListener
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: "+dayOfMonth+"/"+(monthOfYear+1)+"/"+year;
dateTextView.setText(date);
}
使用提供的工厂
创建DatePickerDialogCalendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.show(getFragmentManager(), "Datepickerdialog");
有关演示和详细信息,请访问here。
答案 1 :(得分:0)
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
DatePickerFragmentListener datePickerListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
//this makes current date as the default date of the calander
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public interface DatePickerFragmentListener {
void onDateSet(DatePicker view, int year, int month, int day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day ){
datePickerListener.onDataSet(view,year,month,day)
}
您的活动
public class FilterGuideList extends AppCompatActivity implements DatePickerFragment.DatePickerFragmentListener {
private LinearLayout from_date;
private LinearLayout to_date;
private TextView from_text;
private TextView to_text;
...
....
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//now you will get value of view,year,month and day here , you can put log here
}