3

我正在使用以下方法弹出对话框来选择日期。

private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                dobYear = year;
                dobMonth = monthOfYear;
                dobDay = dayOfMonth;
                if(isEighteenYearOld()){
                   //display the current date
                    dateDisplay();
                } else{
                   Toast.makeText(mContext, "You must be 18 year old", Toast.LENGTH_SHORT).show();
                }  
            } 

        };

我知道 onDateSet 我们可以得到选定的日期。但我正在尝试的是,如果选择的日期小于 18 岁,我需要提醒用户。我已经尝试了上面的代码,但它关闭了对话框并返回到活动。

我想留在对话框中,直到用户选择 18 岁的日期。我不确定如何在对话框中获取 onclick 事件?

4

2 回答 2

6

您正在使用的日期选择器对话框已弃用,因此我建议您不要使用它。

您可以通过两种方式实现日期选择器(据我所知)

  1. 使用 DialogFragment
  2. 使用警报对话框

使用 DialogFragment:

public class MainActivity extends FragmentActivity {
    EditText text;
    Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button)findViewById(R.id.button1);
        text=(EditText)findViewById(R.id.editText1);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                DateDialogFragment datepicker=new DateDialogFragment();
                datepicker.show(getSupportFragmentManager(), "showDate");
            }
        });
    }

    public class DateDialogFragment extends DialogFragment  implements DatePickerDialog.OnDateSetListener{

        public DateDialogFragment()
        {
        }
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Calendar cal=Calendar.getInstance();
            int year=cal.get(Calendar.YEAR);
            int month=cal.get(Calendar.MONTH);
            int day=cal.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            showSetDate(year,monthOfYear,dayOfMonth);
        }

        }

    public void showSetDate(int year,int month,int day) {
    text.setText(year+"/+"+month+"/"+day);
    }
}

检查此示例并在您的 Activity 中实施相同的示例。

使用警报对话框

使用第二个非常简单

在您的 res/layout 文件夹中创建布局并将 DatePicker 放置在布局中

 LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = (View) inflater.inflate(R.layout.yourlayout, null);
DatePicker picker=(DatePicker)view.findViewById(R.id.datepicker);

  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
 builder.setView(view).
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
于 2013-02-22T18:41:22.600 回答
1

如果您不想使用此类对话框的自定义实现,则必须子类DatePickerDialog化以实现此类行为。您不能仅使用 来阻止对话框关闭DatePickerDialog.OnDateSetListener

不幸的是,对话框的实现随着不同的 API 级别而变化,因此通过子类化获得所需的行为并不是那么简单。您需要添加一些技巧以使其可靠地工作。

我创建了一个示例实现,它阻止对话框关闭,除非设置了适当的日期(或者点击取消或返回按钮)。调整它以向用户显示您的警报,最好的地方是onClick()方法中的 else 分支。

class CheckingDatePickerDialog extends DatePickerDialog {

    private int year;
    private boolean cancel = false;
    private boolean isCancelable = true;

    CheckingDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
        this.year = year;
    }

    CheckingDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, theme, callBack, year, monthOfYear, dayOfMonth);
        this.year = year;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // allow closing the dialog with cancel button
        Button btn = getButton(BUTTON_NEGATIVE);
        if (btn != null) {
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel = true;
                    dismiss();
                }
            });
        }
    }

    @Override
    public void setCancelable(boolean flag) {
        isCancelable = false;
        super.setCancelable(flag);
    }

    @Override
    public void onBackPressed() {
        // allow closing the dialog with back button if the dialog is cancelable
        cancel = isCancelable;
        super.onBackPressed();
    }

    private boolean isOldEnough() {
        // test if the date is allowed
        return year <= 1994;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // necessary for some Honeycomb devices
            DatePicker dp = getDatePicker();
            this.year = dp.getYear();
        }

        if (isOldEnough()) {
            // OnDateSetListener is called in super.onClick()
            super.onClick(dialog, which);  
        } else {
            // place your alert here
        }
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        // on some Honeycomb devices called only with the first change
        // necessary for devices running Android 2.x
        this.year = year;
        super.onDateChanged(view, year, month, day);
    }

    @Override
    public void dismiss() {
        if (cancel || isOldEnough()) {
            // do not allow the dialog to be dismissed unless a cancel or back button was clicked
            super.dismiss();
        }
    }
};
于 2013-02-22T17:29:21.247 回答