实施日期选择器的问题

时间:2013-11-01 06:29:21

标签: java android datepicker

在我的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 实施。 任何人都可以解释如何解决这个问题。

提前完成

0 个答案:

没有答案