1

我有一个 FromDate EditText,我已向其中添加了图像。现在,当我单击图像时,Datepicker会显示一个。现在,当我选择某个日期并单击Set Button时,日期设置在EditText. 问题是,当我单击 Cancel 时Button,日期选择器消失了,并且在我按 Back 之前,prev 活动不会获得焦点!屏幕上没有触摸事件。请帮忙...

4

2 回答 2

7

我不相信这可能是最好的方法......但是下面的代码是如何工作的;从此链接获得了一些指导 https://stackoverflow.com/a/4981308/840520

    dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
               if (which == DialogInterface.BUTTON_NEGATIVE) {                 
                  dialog.dismiss();
                  onBackPressed();
               }
            }
          });   

由于我有一个项目交付,现在这对我有用。但是,我仍在寻找一种体面的方式来处理这个问题。android平台是否不像其他对话框那样处理取消按钮?

于 2012-06-13T06:23:41.087 回答
1

我已经实现了具有取消事件处理和使用 EditText 的日期选择器。可能对想要清除取消事件文本的人有所帮助。

private EditText dob;
    static final int DATE_DIALOG_ID = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.browse);


        dob = (EditText) findViewById(R.id.txtDob);
                /* For DOB EditText */
        dob.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (v == dob)
                    showDialog(DATE_DIALOG_ID);
                return false;
            }
        });
}


    @Override
    protected Dialog onCreateDialog(int id) {
        final int cyear, cmonth, cday;

        if (dob.getText().equals("")) {
            Calendar c = Calendar.getInstance();
            cyear = c.get(Calendar.YEAR);
            cmonth = c.get(Calendar.MONTH);
            cday = c.get(Calendar.DAY_OF_MONTH);
        } else {
            String[] datestr = dob.getText().toString().split("-");
            cyear = Integer.parseInt(datestr[0]);
            cmonth = Integer.parseInt(datestr[1]);
            cday = Integer.parseInt(datestr[2]);
        }
        switch (id) {
        case DATE_DIALOG_ID:
            // return new DatePickerDialog(this, mDateSetListener, cyear,
            // cmonth,cday);
            DatePickerDialog dateDlg = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {
                        public void onDateSet(DatePicker view, int year,
                                int monthOfYear, int dayOfMonth) {
                            String date_selected = String.valueOf(year)
                                    + "-"
                                    + ((String.valueOf(monthOfYear + 1)
                                            .length() == 1 ? "0"
                                            + String.valueOf(monthOfYear + 1)
                                            : String.valueOf(monthOfYear + 1)))
                                    + "-"
                                    + ((String.valueOf(dayOfMonth).length() == 1 ? "0"
                                            + String.valueOf(dayOfMonth)
                                            : String.valueOf(dayOfMonth)));
                            dob.setText(date_selected);

                        }

                    }, cyear, cmonth, cday);
            dateDlg.setTitle(getString(R.string.dob));
            dateDlg.setCancelable(false);
            dateDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                         dob.setText("");
                         dialog.dismiss();

                        }
                    });

            return dateDlg;

        }
        return null;
    }
于 2013-05-09T06:45:18.673 回答