android - 如何从 dialogfragment 获取日期选择器属性

标签 android datepicker

你好,我有这个代码

public class DateTimePicker extends android.support.v4.app.DialogFragment  {

 public interface DateTimePickerListener {
       // public void onDialogPositiveClick(DialogFragment dialog);
      //  public void onDialogNegativeClick(DialogFragment dialog);
        public void onDialogPositiveClick(android.app.DialogFragment dialog);
        public void onDialogNegativeClick(android.app.DialogFragment dialog);
    }

    // Use this instance of the interface to deliver action events
   DateTimePickerListener mListener;
   DatePicker dp;
   TimePicker tp;
   int year;

   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
       AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
       // Get the layout inflater
       LayoutInflater inflater = getActivity().getLayoutInflater();
       dp = (DatePicker) this.getActivity().findViewById(R.id.date_picker);

     //  tp = (TimePicker) this.getView().findViewById(R.id.time_picker);
       // Inflate and set the layout for the dialog
       // Pass null as the parent view because its going in the dialog layout
       builder.setView(inflater.inflate(R.layout.date_time_picker, null))
       // Add action buttons
              .setPositiveButton(R.string.set, new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                      // sign in the user ...

                   year = dp.getDayOfMonth();


                   StringBuilder sb = new StringBuilder();
                   sb.append(year);


           /*  int   day  = dp.getDayOfMonth();
             int   month= dp.getMonth()+1;
             int   year = dp.getYear();

             SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

            Date dt = new Date(day, month, year);

             String formatedDate = sdf.format(dt);*/
              ServiceActivity.tv.setText(sb.toString());
                   Toast.makeText(getActivity().getBaseContext(), "You selected set button ",
                            Toast.LENGTH_LONG).show();
                  }
              })
              .setNegativeButton(R.string.cansel, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                      DateTimePicker.this.getDialog().cancel();
                  }
              });  

       return builder.create();
   }

    // Override the Fragment.onAttach() method to instantiate the DateTimePickerListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (DateTimePickerListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement DateTimePickerListener");
        }
    }

    } 

当我点击设置按钮时,程序崩溃了。我试过了
dp = (DatePicker) this.getView().findViewById(R.id.date_picker);

它崩溃了我试过了

dp = (DatePicker) this.dialog.findViewById(R.id.date_picker);

为什么会崩溃?

最佳答案

onCreateDialog 中尝试这段代码:

LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.date_time_picker, null );
dp = (DatePicker) view.findViewById(R.id.datePicker);
builder.setView(view, null))

关于android - 如何从 dialogfragment 获取日期选择器属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31071039/

相关文章:

java - 错误 android.view.InflateException : Binary XML: Error inflating class Button caused by xml background

android - Renderscript 未使用构建工具 25.0.0+ 进行编译

JavaFX DatePicker 设置日期值的对齐方式

c# - 从 DatePicker 中以 yyyy/MM/dd 格式获取日期

angular - 如何在对话框流参数中添加日期选择器

Android/IOS Secret 到期管理与客户端凭据流程

java - 错误:Execution failed for task ':app:transformClassesWithAndroidGradleClassShrinkerForDebug'

javascript - DatePicker 开始日期和结束日期验证,无需插件

Jquery datepicker 仅可选择 1 天,并在下一个输入中复制日期 +x 天

android - 成绩同步失败 : Unresolved dependencies