如何在 EditText 中设置日期

3

我有一个编辑文本框,调用了ontouchlistener后会显示一个自定义对话框。当我点击setdate按钮时,日期选择器上的日期应设置在编辑文本框中,并且对话框应该关闭。但我不知道如何从日期选择器中获取日期并将其设置在编辑文本框中。我在date.init(year,monthOfYear,dayOfMonth,new MyOnDateChangedListener())中遇到错误,错误是:Multiple markers at this line - dayOfMonth cannot be resolved to a variable - year cannot be resolved to a variable - monthOfYear cannot be resolved to a variable

 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;
    }

请在编写代码时进行缩进。 - Stefano Ortisi
2个回答

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);

现在将这两个函数添加到调用日期选择对话框并设置日期到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中设置日期。希望这对您有所帮助。

如何在未选择日期时将错误设置为EditText?对我来说,只显示错误图标! - Kanagalingam

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;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接