how to set date in edit text

别来无恙 提交于 2020-01-02 07:33:41

问题


i have a edit text box and i have call ontouchlistener it show a custom dialog and when i click setdate button the date on date picker should set on the edit text and the dialog should get dismiss. but i dont know how to get date from date picker and how to set in edit text box. i am getting error in date.init( year, monthOfYear, dayOfMonth, new MyOnDateChangedListener() ); and the error isMultiple 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;
    }

回答1:


Define editText and Button in xml and below code in your activity:

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

Now add these two functions to call DatePicker Dialog and to set date in the 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));

        }

    };

Define this globally:

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

This will help you to set date in the editText.Hope this will help you.




回答2:


You have to use 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);

            }
        };

create Dialog as usual

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


来源:https://stackoverflow.com/questions/11360915/how-to-set-date-in-edit-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!