我有一个Android活动,点击EditText对话框会弹出一个日期选择器。但是当我设置日期时,它在编辑文本上显示不正确。
这是我所说的一个例子:
以下是我使用的代码:
public class CreateTermWindow extends Activity {
EditText tname, tstart, tend;
Button tcreate;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd", Locale.CANADA);
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_term_window);
tname = (EditText) findViewById(R.id.et_newTerm_name);
tstart = (EditText) findViewById(R.id.et_newTerm_start);
tend = (EditText) findViewById(R.id.et_newTerm_end);
tcreate = (Button) findViewById(R.id.btn_newTerm_create);
tstart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(CreateTermWindow.this, c1, calendar1
.get(Calendar.YEAR), calendar1.get(Calendar.MONTH),
calendar1.get(Calendar.DATE)).show();
}
});
tend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(CreateTermWindow.this, c2, calendar2
.get(Calendar.YEAR), calendar2.get(Calendar.MONTH),
calendar2.get(Calendar.DATE)).show();
}
});
tcreate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = new DatabaseHelper(getBaseContext())
.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", tname.getText().toString());
values.put("start", tstart.getText().toString());
values.put("end", tend.getText().toString());
db.insert("termTable", null, values);
Toast.makeText(getApplicationContext(), "New Term Created!",
Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.create_term_window, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void updateLabel(EditText e, Calendar c) {
e.setText(sdf.format(c.getTime()));
}
DatePickerDialog.OnDateSetListener c2 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar2.set(Calendar.YEAR, year);
calendar2.set(Calendar.MONTH, monthOfYear);
calendar2.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel(tend, calendar2);
}
};
DatePickerDialog.OnDateSetListener c1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar1.set(Calendar.YEAR, year);
calendar1.set(Calendar.MONTH, monthOfYear);
calendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel(tstart, calendar1);
}
};
}
答案 0 :(得分:0)
代替此代码,(您引用updateLabel方法):
DatePickerDialog.OnDateSetListener c1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar1.set(Calendar.YEAR, year);
calendar1.set(Calendar.MONTH, monthOfYear);
calendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel(tstart, calendar1);
}
};
试试这个:
DatePickerDialog.OnDateSetListener c1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar1.set(Calendar.YEAR, year);
calendar1.set(Calendar.MONTH, monthOfYear);
calendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
tstart.setText(String.valueOf(year) + "/"
+ String.valueOf(monthOfYear + 1) + "/" + String.valueOf(dayOfMonth));
}
};
答案 1 :(得分:-1)
SimpleDateFormat
的格式字符串使用小写字母m来表示分钟而不是月份。
格式字符串应更改为:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CANADA);
另见: