0

在我的应用程序中,我想让用户从日期选择器中选择日期。我搜索并完成了以下代码。但它显示错误。请帮助我。

我的代码:

 save.setOnClickListener(new View.OnClickListener() 
            {               
                @Override
                public void onClick(View arg0) 
                {
                    // TODO Auto-generated method stub
                        EditText item=(EditText)findViewById(R.id.itemedit);
                        EditText price=(EditText)findViewById(R.id.priceedit);
                        EditText rec=(EditText)findViewById(R.id.reccedit);
                        EditText date=(EditText)findViewById(R.id.reccdateedit);
                        EditText type=(EditText)findViewById(R.id.recccatedit);

                        String upditem=item.getText().toString();
                        String updprice=price.getText().toString();
                        String dateedit=date.getText().toString();
                        String typeedit=type.getText().toString();


                        showDialog(DATE_DIALOG_ID);------------>I don't konw exactly about this.But i got this code.
                        System.out.println("DP="+mYear);

}




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

显示错误:

cannot create dialog for 0;

有没有其他方法可以做到这一点。

4

2 回答 2

1

我使用这样的东西:

date.setKeyListener(null);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
// Set current date by default
updateDateField(date, day, month, year);
final DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        updateDateField(date, dayOfMonth, monthOfYear, year);
    }
};
final DatePickerDialog dialog = new DatePickerDialog(context, mDateSetListener, year, month, day);
dialog.setTitle(context.getResources().getString(R.string.lib_date));
date.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.show();
        }
    }
});


public static void updateDateField(EditText date, int day, int month, int year) {
    StringBuilder sb = new StringBuilder();
    if (day < 10)
        sb.append("0" + day);
    else
        sb.append(day);
    if (month < 10)
        sb.append("/0" + month + "/");
    else
        sb.append("/" + month + "/");
    sb.append(year);
    date.setText(sb.toString());
}

其中 date 是我要编辑的 EditText。

于 2012-08-10T11:15:45.223 回答
0

我认为这对你很有帮助 http://www.bogotobogo.com/Android/android8DatePickerTimePickerClocks.php#DatePicker

于 2012-08-10T11:33:11.930 回答