在我的 Android 应用程序中,当我单击按钮时,日期选择器会出现。我想在文本视图中设置选定的日期。但是当我在日期选择器上设置日期并单击完成按钮时,它不会设置文本视图。这是我的尝试。
final Calendar c = Calendar.getInstance();
Year = c.get(Calendar.YEAR);
Month = c.get(Calendar.MONTH);
Day = c.get(Calendar.DAY_OF_MONTH);
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.btnDate:
CustomDialogFragment dialog = new CustomDialogFragment(
NewComplain.this);
Bundle bundle = new Bundle();
bundle.putInt("dialog_id", DATE_DIALOG_ID);
bundle.putInt("year", Year);
bundle.putInt("month", Month);
bundle.putInt("day", Day);
dialog.setArguments(bundle);
dialog.show(getFragmentManager(), "dialog");
break;
}
}
我的 CustomDialogFragment 类是这样的。
package xont.virtusel.v4.controller.sale;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
public class CustomDialogFragment extends DialogFragment {
public static final int DATE_PICKER = 1;
public static final int TIME_PICKER = 2;
public static final int DIALOG = 3;
private OnDateSetListener listener_;
Context mContext;
String mMessage;
boolean finishActivity;
// private Fragment mCurrentFragment;
Activity mActivity;
/**
* this constructor is used for datepicker & Timepicker
*/
public CustomDialogFragment(Fragment fragment) {
// mCurrentFragment = fragment;
}
public CustomDialogFragment(Activity mActivity) {
this.mActivity = mActivity;
}
/**
* this constructor is used for simple dialog fragment
*/
public CustomDialogFragment(Context context, String message,
final boolean finishActivity) {
mContext = context;
mMessage = message;
this.finishActivity = finishActivity;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = new Bundle();
bundle = getArguments();
int id = bundle.getInt("dialog_id");
switch (id) {
case DATE_PICKER:
return new DatePickerDialog(getActivity(),
(OnDateSetListener) mActivity, bundle.getInt("year"),
bundle.getInt("month"), bundle.getInt("day"));
}
// Define your custom dialog or alert dialog here and return it.
return new Dialog(getActivity());
}
}
在我的活动类中,我实现了以下代码段以在文本视图中设置选定的日期。
private void updateDisplay() {
System.out.println("===updateDisplay===");
String str_date = "";
if ((Month + 1) >= 10) {
if (Day < 10) {
str_date = Year + "-" + (Month + 1) + "-0" + Day;
} else {
str_date = Year + "-" + (Month + 1) + "-" + Day;
}
} else {
if (Day < 10) {
str_date = Year + "-0" + (Month + 1) + "-0" + Day;
} else {
str_date = Year + "-0" + (Month + 1) + "-" + Day;
}
}
date.setText("" + str_date);
}
@Override
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
System.out.println("1233");
Year = year;
Month = monthOfYear;
Day = dayOfMonth;
updateDisplay();
}
我的活动由OnDateSetListener实现。任何人都可以解释如何解决这个问题。
提前感谢