0

我有一个用户必须输入出生日期的表格,有一个选择日期的按钮,当用户单击设置时,按钮将显示用户的结果,但我只有一个日期而不是时间,

private SQLiteDatabase database;
private DBHelper helper;
Button btnSave, btnDOB;
EditText addNama, addAddress, addBloodType, addHeight, addDrName, addDrContact,
         addGuardianName1, addGuardianContact1, addGuardianName2, 
         addGuardianContact2, addHospitalName, addHospitalContact;

DateFormat fmtDOBDate = DateFormat.getDateTimeInstance();
TextView lblDOBDate;
Calendar myDOBCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
myDOBCalendar.set(Calendar.YEAR, year);
myDOBCalendar.set(Calendar.MONTH, monthOfYear);
myDOBCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelDOBDate();
}
};

private void updateLabelDOBDate() {
    lblDOBDate.setText(fmtDOBDate.format(myDOBCalendar.getTime()));
}

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

    btnSave = (Button)findViewById(R.id.btnSave);
    btnDOB = (Button)findViewById(R.id.dobbtn);
    addNama = (EditText)findViewById(R.id.editText1);
    addAddress = (EditText)findViewById(R.id.editText4);
    addBloodType = (EditText)findViewById(R.id.editText5);
    addHeight = (EditText)findViewById(R.id.editText6);
    addDrName = (EditText)findViewById(R.id.editText7);
    addDrContact = (EditText)findViewById(R.id.editText8);
    addGuardianName1 = (EditText)findViewById(R.id.editText9);
    addGuardianContact1 = (EditText)findViewById(R.id.editText10);
    addGuardianName2 = (EditText)findViewById(R.id.EditText01);
    addGuardianContact2 = (EditText)findViewById(R.id.EditText02);
    addHospitalName = (EditText)findViewById(R.id.editText11);
    addHospitalContact = (EditText)findViewById(R.id.editText12);

    helper = new DBHelper(this);
    database = helper.getWritableDatabase();

    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            String name = addNama.getText().toString();
            String dob = btnDOB.getText().toString();
            String address = addAddress.getText().toString();
            String bloodtype = addBloodType.getText().toString();
            String height = addHeight.getText().toString();
            String drname = addDrName.getText().toString();
            String drcontact = addDrContact.getText().toString();
            String guardianname1 = addGuardianName1.getText().toString();
            String guardiancontact1 = addGuardianContact1.getText().toString();
            String guardianname2 = addGuardianName2.getText().toString();
            String guardiancontact2 = addGuardianContact2.getText().toString();
            String hospitalname = addHospitalName.getText().toString();
            String hospitalcontact = addHospitalContact.getText().toString();

            helper.insertDataProfile(database, name, dob, address, bloodtype, height, 
                    drname, drcontact, guardianname1, guardiancontact1,
                    guardianname2, guardiancontact2, hospitalname,
                    hospitalcontact);
            Toast.makeText(ProfileAdd.this, "Successfully Add", Toast.LENGTH_SHORT).show();

            Intent i = new Intent(ProfileAdd.this, ProfileMain.class);
            startActivity(i);

        }
    });


    lblDOBDate = (TextView) findViewById(R.id.dobbtn);
    Button btnStartDate = (Button) findViewById(R.id.dobbtn);
    btnStartDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new DatePickerDialog(ProfileAdd.this, d, myDOBCalendar
                    .get(Calendar.YEAR), myDOBCalendar.get(Calendar.MONTH),
                    myDOBCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

    updateLabelDOBDate();
}

public void onClickCancel(View v){
    startActivity (new Intent(getApplicationContext(), ProfileMain.class));
}

当我只想显示时间时也是如此..请帮帮我

4

1 回答 1

0

你需要实现如下:

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

            // when dialog box is closed, below method will be called.
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {
                int year = selectedYear;
                int month = selectedMonth;
                int day = selectedDay;

                your_button.setText(new StringBuilder().append(year).append("-")
                        .append(month + 1).append("-").append(day).append(" "));


            }
        };

像这样创建一个新的 DatePicker Dialog 对象:

Calendar cal=Calendar.getInstance();

        year=cal.get(Calendar.YEAR);
        month=cal.get(Calendar.MONTH);
        day=cal.get(Calendar.DATE);


 new DatePickerDialog(this, datePickerListener, year, month,day);  //Call this on button click
于 2013-03-02T13:58:22.993 回答