如何更新datepiker对话框Android的日期? [英] How to update the Date in datepiker dialog android?

查看:171
本文介绍了如何更新datepiker对话框Android的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**您好,
我开发的应用程序在Android中,其中我使用DatePiker对话框,我试图

**hello, I am developing application in android in which i am using DatePiker dialog and i tried

@override

保护对话框onCreateDialog(INT ID){

开关(ID){

    case DATE_DIALOG_ID:
    {
        if(tvDate.getText().toString().equalsIgnoreCase("--"))
        {
            Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
        }
        else
        {
            String _date[] = tvDate.getText().toString().split("-");
            String _monthName = _date[0];
            mMonth = helper.getMonthFromName(_monthName) -1;
            mDay = Integer.parseInt(_date[1]);
        }
            return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);
    }

        return null;
}

@override

私人DatePickerDialog.OnDateSetListener mDateSetListener =
        新DatePickerDialog.OnDateSetListener(){

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDate();
        if(tvRepeat.getText().toString().equalsIgnoreCase("Weekly")) {

                        tvDayName.setText(" (" + getDayNameForWeeklyRepeat() + ")");
        }
    }
};

私人无效updateDate(){

    tvDate.setText(new StringBuilder()
            // Month is 0 based so add 1
        .append(DateUtils.getMonthString(mMonth,DateUtils.LENGTH_SHORT))
                    .append("-")
        .append(pad(mDay)).append("-")
        .append(pad(mYear%100)));
         }

所以 onCreateDialog 方法调用一次,但是当我的下一个记录则点击DatePiker对话框显示敢其中显示第一次的纪录,但我想新纪录的显示日期,请帮助我

so onCreateDialog method is calling only once but when i click on next record then the DatePiker dialog is displaying the dare of the record which is displayed first time but i want to display date of new record please help me

推荐答案

从引用的 http://developer.android.com/guide/tutorials/views/hello-datepicker.html

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // capture our View elements
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        mPickDate = (Button) findViewById(R.id.pickDate);

        // add a click listener to the button
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // display the current date
        updateDisplay();
    }

重写onCreateDialog

Override the onCreateDialog

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
    }
    return null;
}

如何更新显示:

// updates the date we display in the TextView
    private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" "));
    }

这篇关于如何更新datepiker对话框Android的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆