日期选择器与片段级的android [英] date picker with fragment class android
问题描述
我正在开发的抽屉式导航栏的应用程序。当我在导航列表中单击一些项目它显示使用片段一些接口。和我的点击物品也正常工作。我有使用图像按钮,显示日期选择器。下面我有codeS使用。但是当我设置为从数据采集器对话框的日期。它不会改变给定的文本视图。请指导我一些code样品。
i am developing an application with the navigation drawer. when i click some items in the navigation list it shows some interface using a fragment. and my click items also working properly. i have use an image button to show a date picker. i have use below codes. but when i set an date from data picker dialog. it does not change the given text view. please guide me with some code samples..
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
}
}
推荐答案
创建使用参数的构造是一个坏主意。
Creating a constructor with params is a bad idea.
使用一个接口作为回调到活动中,并设置选择TextView的日期在活动或framgent。
Use a interface as a call back to the activity and set the date selected to textview in activity or framgent.
从DialogFragment到活动
From DialogFragment to Activity
<一个href=\"http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment\">How要格式化的日期字符串从我DatePickerFragment转移?
活动到碎片
<一个href=\"http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android\">Send从活动数据在Android中片段
DialogFragment - >活动 - >片段
DialogFragment-->Activity-->Fragment
http://developer.android.com/training/basics/fragments/ communicating.html
这篇关于日期选择器与片段级的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!