我在Date Picker
中使用了Activity
,但我不知道如何在Fragment中使用它。我正在上传我的活动Date Picker
代码。任何人都可以帮我转换片段中的代码?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
context = getApplicationContext();
View.OnClickListener listenerDate = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID);
}
};
editTextDate.setOnClickListener(listenerDate);
} private void initialize() {
editTextDate = (EditText) findViewById(R.id.editTextDate);
}private void updateDisplay() {
currentDate = new StringBuilder().append(day).append(".")
.append(month + 1).append(".").append(year).toString();
Log.i("DATE", currentDate);
}
DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int j, int k) {
year = i;
month = j;
day = k;
updateDisplay();
editTextDate.setText(currentDate);
}
};`
答案 0 :(得分:3)
试试这个样本:
public class YourClass extends Fragment {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.your_layout, container, false);
return view;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
edittext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDatePicker();
}
});
}
private void showDatePicker() {
DatePickerFragment date = new DatePickerFragment();
/**
* Set Up Current Date Into dialog
*/
Calendar calender = Calendar.getInstance();
Bundle args = new Bundle();
args.putInt("year", calender.get(Calendar.YEAR));
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
date.setArguments(args);
/**
* Set Call back to capture selected date
*/
date.setCallBack(ondate);
date.show(getFragmentManager(), "Date Picker");
}
OnDateSetListener ondate = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
edittext.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear+1)
+ "-" + String.valueOf(year));
}
};
}
现在创建类DatePickerFragment:
public class DatePickerFragment extends DialogFragment {
OnDateSetListener ondateSet;
private int year, month, day;
public DatePickerFragment() {}
public void setCallBack(OnDateSetListener ondate) {
ondateSet = ondate;
}
@SuppressLint("NewApi")
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
year = args.getInt("year");
month = args.getInt("month");
day = args.getInt("day");
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
}
}
希望它有所帮助。
答案 1 :(得分:0)
您可以在片段中使用它:
Calendar myCalendar = Calendar.getInstance() //global
public void showDatePickerDialog(){
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateYourLabel(); // or whatever you want
}
};