在我的应用程序中,我有 2 个按钮打开时间对话框/日期对话框,然后我选择日期和时间,然后按确定。因此该值显示在 TextView 中(对于每个),所以我需要从 TextViews 中获取 VALUE 并将其保存在数据库中。
数据库行=date
TextView 中显示的时间代码:
tvHora = (TextView) findViewById(R.id.tvHora);
final Calendar d = Calendar.getInstance();
hour = d.get(Calendar.HOUR_OF_DAY);
minute = d.get(Calendar.MINUTE);
//set current time into textview
tvHora.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
TextView 中显示的日期代码:
tvData = (TextView) findViewById(R.id.tvData);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
//SET CURRENT DATE INTO TEXTVIEW
tvData.setText(new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day).append("-"));
单击“保存”按钮时的代码:
ContentValues valor = new ContentValues();
valor.put("mensagenssalvas", resultado);
db.insert("mensagens", null, valor);
那么如何从文本视图中获取值以保存在数据库中呢?
编辑 时间选择器对话框:
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
// TODO Auto-generated method stub
hour = selectedHour;
minute = selectedMinute;
//set current time into textview
tvHora.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
}
};
日期选择器对话框:
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int Year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
year = Year;
month = monthOfYear;
day = dayOfMonth;
// set selected date into textview
tvData.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};