Android Studio:使用自定义对话框向活动添加日期,时间和任务

时间:2018-12-11 23:17:11

标签: java android android-studio

嗨,我是android studio的新手,我想通过自定义将日期,时间和任务名称添加到我的主要活动中。尝试实现日期选择器对话框和时间选择器对话框时,我的代码出现编译错误。

public class AddTaskDialog extends AppCompatDialogFragment implements
        View.OnClickListener {

    Button btnDatePicker, btnTimePicker;
    EditText txtDate, txtTime, txtTask;
    private int mYear, mMonth, mDay, mHour, mMinute;
    private AddTaskDialogListener listener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.addtask, null);

        builder.setView(view)
                .setTitle("Add Task")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String tTitle = txtTask.getText().toString();
                        String tDate = txtDate.getText().toString();
                        String tTime = txtTime.getText().toString();
                        listener.applyTexts(tTitle,tDate,tTime);

                    }
                });

        btnDatePicker=(Button)view.findViewById(R.id.btn_date);
        btnTimePicker=(Button)view.findViewById(R.id.btn_time);
        txtDate=(EditText)view.findViewById(R.id.in_date);
        txtTime=(EditText)view.findViewById(R.id.in_time);
        txtTask=(EditText)view.findViewById(R.id.taskname);

        btnDatePicker.setOnClickListener(this);
        btnTimePicker.setOnClickListener(this);
        return builder.create();
    }

    @Override
    public void onClick(View v) {

        if (v == btnDatePicker) {

            // Get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

我的问题在这里:

        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                    }
                }, mYear, mMonth, mDay);
        datePickerDialog.show();
    }
    if (v == btnTimePicker) {

            // Get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

还有

// Launch Time Picker Dialog
            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {
                            txtTime.setText(hourOfDay + ":" + minute);
                        }
                    }, mHour, mMinute, false);
            timePickerDialog.show();

        }

        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            try {
                listener = (AddTaskDialogListener)context;
            } catch (ClassCastException e) {
                throw  new ClassCastException(context.toString()+"must implement listner");
            }
        }

        public interface AddTaskDialogListener{
            void applyTexts (String tTitle, String tDate, String tTime);

        }
    }

Logcat不会告诉我任何有用的信息,只是它是编译错误。我无法将对话框添加到对话框吗?如果是这样,请推荐我该怎么做。

0 个答案:

没有答案