如何在 DatePickerDialog.OnDateSetListener 中获取 OnClick? [英] How to get OnClick in DatePickerDialog.OnDateSetListener?

查看:13
本文介绍了如何在 DatePickerDialog.OnDateSetListener 中获取 OnClick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的方法来弹出选择日期的对话框.

I am using the following method to pop up the dialog to pick the date.

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 岁,我需要提醒用户.我已经尝试了上面的代码,但它关闭了对话框并返回到活动.

I know that onDateSet we can get the selected date. But what i am trying is if the date selected is younger than 18 years i need to alert the user. I have tried above code but it close the dialog and gone back to the activity.

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

I want to stay in the dialog until user selects the date which is 18 years old.I am not sure how to get the onclick event in the dialog?

推荐答案

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

Date Picker Dialog you are using is deprecated so I recommend you to don't use that.

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

You can implement the Date picker in two ways(that I know of)

  1. 使用 DialogFragment
  2. 使用 AlertDialog

使用 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);
    }
}

检查此示例并在您的活动中实现相同的内容.

Check this sample and implement the same in your Activity.

使用警报对话框:

使用第二个非常简单

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

Create layout in your res/layout folder and place DatePicker in the layout

 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();

这篇关于如何在 DatePickerDialog.OnDateSetListener 中获取 OnClick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆