我正在使用导航抽屉开发应用程序。当我点击导航列表中的某些项目时,它会显示一些使用片段的界面。我的点击项也正常工作。我使用图像按钮来显示日期选择器。我使用下面的代码。但是当我从数据选择器对话框设置日期时。它不会更改给定的文本视图。请指导我一些代码示例..
public class FindPeopleFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public int year;
public int month;
public int day;
static final int DATE_PICKER_ID = 1111;
TextView curentDate;
public FindPeopleFragment(TextView date){
curentDate = date;
}
public FindPeopleFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.schedule, container, false);
ImageButton date = (ImageButton) rootView.findViewById(R.id.btnDate);
curentDate = (TextView) rootView.findViewById(R.id.txtCurrentDate);
/*final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
curentDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));*/
date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(getActivity(), "clicked", Toast.LENGTH_LONG).show();
//showDialog(DATE_PICKER_ID);
showDatePickerDialog(v);
}
});
return rootView;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), datePickerListener, year, month, day);
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener(){
public void onDateSet(DatePicker view, int syear, int smonth, int sday) {
// Do something with the date chosen by the user
month = smonth;
day = sday;
year = syear;
curentDate.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(year));
}
};
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
使用params创建构造函数是个坏主意。
使用界面作为对活动的回调,并将选择的日期设置为活动或framgent中的textview。
从DialogFragment到Activity
How to transfer the formatted date string from my DatePickerFragment?
片段活动
Send data from activity to fragment in android
DialogFragment - >活动 - >片段
http://developer.android.com/training/basics/fragments/communicating.html