【问题标题】:Add Neutral Button in DialogFragment which implements DatePickerDialog在实现 DatePickerDialog 的 DialogFragment 中添加中性按钮
【发布时间】:2014-02-04 02:00:57
【问题描述】:

正如您在下面的代码中看到的那样,我尝试添加一个中性按钮来设置query_date 并关闭对话框。不幸的是,下面的这个不起作用:中性按钮根本不显示。

我想要一个名为“今天”的中间按钮并执行该操作。

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setNeutralButton("Today", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    query_date = now_date;
                    dialog.dismiss();
                }
            });

            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
            dialog.getDatePicker().setMinDate(c.getTimeInMillis() );
            return dialog;
        }

        @SuppressLint("SimpleDateFormat")
        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
            TimeZone tz = TimeZone.getDefault();
            SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");                
            sdfSource.setTimeZone(tz);
            Date date = new GregorianCalendar(year,month,day).getTime();
            SimpleDateFormat sdfDestination = new SimpleDateFormat("EEE', 'd LLL");

            query_date = sdfSource.format(date);
            change_date_button.setText(sdfDestination.format(date));

            load_more_bar.setVisibility(View.VISIBLE);
            new get_events().execute();
        }

    }

我从 onClick 按钮侦听器调用 DialogFragment 的方式如下:

DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getActivity().getFragmentManager(), "datePicker"); 

DatePickerDialog 片段中是否可以有一个中性按钮?如果有怎么办?

【问题讨论】:

    标签: android android-dialog android-dialogfragment android-datepicker


    【解决方案1】:

    您正在实例化两个对话框。一个 AlertDialog 和另一个 DatePickerDialog。你应该只使用一个:

    @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    
     final Calendar c = Calendar.getInstance();
     int year = c.get(Calendar.YEAR);
     int month = c.get(Calendar.MONTH);
     int day = c.get(Calendar.DAY_OF_MONTH);
    
     DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
     dialog.getDatePicker().setMinDate(c.getTimeInMillis() );
     dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Today", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {
             query_date = now_date;
             dialog.dismiss();
         }
     });
     return dialog;
    

    }

    【讨论】:

    • 此解决方案似乎不适用于所有设备。我有一台搭载 Android 7.0 的华为 MediaPad T3,但看不到中性按钮。在其他设备中,该按钮工作正常。我想解决方法是使用 onShowListener 和 findViewById 来使用中性按钮。 (虽然我没试过)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 2013-08-30
    • 2014-12-13
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2020-08-11
    相关资源
    最近更新 更多