如何在编辑文本中设置日期

时间:2012-07-06 10:58:30

标签: android datepicker

我有一个编辑文本框,我调用ontouchlistener它显示一个自定义对话框,当我点击设置日期按钮时,日期选择器上的日期应该在编辑文本上设置,对话框应该被解雇。但我不知道如何从日期选择器获取日期以及如何在编辑文本框中设置。我在date.init(year,monthOfYear,dayOfMonth,new MyOnDateChangedListener())中收到错误;并且错误是此行的多个标记      - dayOfMonth无法解析为      变量      - 年份无法解决变量      - monthOfYear无法解析为      变量

 et4.setOnTouchListener(new OnTouchListener() {
    final Dialog setdatedialog = new Dialog(DropboxActivity.this);                      
    public void onClick(View v) {
    }
    public boolean onTouch(View arg0, MotionEvent arg1) {
    setdatedialog.setContentView(R.layout.datedialog);
    setdatedialog.setTitle("select date of puchase");
    setdatedialog.setCancelable(true);
    setdatedialog.show();
    Button back = (Button)setdatedialog.findViewById(R.id.back3);
    back.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    setdatedialog.dismiss();
    }
    });
    Button setdate=(Button)setdatedialog.findViewById(R.id.setdate);
    DatePicker date = (DatePicker)setdatedialog.findViewById(R.id.datePicker1);                         
    class MyOnDateChangedListener implements OnDateChangedListener {
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){
            et4.setText( "" + dayOfMonth + "-" + (monthOfYear + 1) + "-" + year );
    }
    };
    date.init( year, monthOfYear, dayOfMonth, new MyOnDateChangedListener() );

    return false;
    }

2 个答案:

答案 0 :(得分:4)

在活动中的xml及以下代码中定义editText和Button:

        editText=(EditText) findViewById(R.id.date);
        button=(Button) findViewById(R.id.play);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        Calendar c=Calendar.getInstance();
        mYear=c.get(Calendar.YEAR);
        mMonth=c.get(Calendar.MONTH);
        mDay=c.get(Calendar.DAY_OF_MONTH);

现在添加这两个函数来调用DatePicker对话框并在Edittext

中设置日期
protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);

        }

        return null;

    }
    private DatePickerDialog.OnDateSetListener mDateSetListener =new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            editText.setText(new StringBuilder().append(mDay).append("/").append(mMonth+1).append("/").append(mYear));

        }

    };

全球定义:

    static final int DATE_DIALOG_ID = 0;
    private int mYear,mMonth,mDay;

这将帮助您在editText.Hope中设置日期,这将对您有所帮助。

答案 1 :(得分:0)

您必须使用DatePickerDialog.OnDateSetListener

 private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                String date_selected = (monthOfYear + 1) + "/" + dayOfMonth + "/"
                        + year;

                editText.setText(date_selected);

            }
        };

像往常一样创建对话

 @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case 1:
                return new DatePickerDialog(this, mDateSetListener, cyear, cmonth,
                        cday);
            }
            return null;
        }