我创建了一个应用程序,其中有3个微调器(下拉),一个是几天,第二个是几个月,第三个是多年。我想将用户选择的值存储在数据库(sqlite)中例子3/1992年8月。我怎样才能实现它。
答案 0 :(得分:1)
你可以这样做: -
spinner= (Spinner)findViewById(R.id.spinnerId);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View v,int position,long id)
{
spinner.setSelection(position);
String selectedRole = (String) spinner.getSelectedItem();
Log.v("", "Spinner position: "+position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
这适用于一个Drop / down,U可以使用相同的两个。
第二个选项: -
如果您愿意,可以将DatePicker用作对话框,如下所示: -
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(this, pickerListener, year, month, day);
}
return null;
}
并且
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker dp, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
Calendar c = Calendar.getInstance();
c.set(selectedYear, selectedMonth, selectedDay);
String strDate = GlobalVariables.formatter4Display.format(new Date(
c.getTimeInMillis()));
tvDob.setText(strDate);
}}
我希望这会对你有所帮助。