0

我已经写了这段代码。它不会给出错误,但会给出错误的输出。日期显示在时间地点和时间地点。我还要设置闹钟。我正在做一个待办事项应用程序

请帮我纠正我的代码并设置警报..

我的代码:

public class Addmytask extends Activity{

static final int DATE_DIALOG_ID = 0;
private int mYear,mMonth,mDay;
EditText editText;

static final int TIME_DIALOG_ID=1;
 int hour,min;

 protected void onCreate(Bundle savedInstanceState) {
     // TODO Auto-generated method stub
     super.onCreate(savedInstanceState);
    setContentView(R.layout.addmytask);

     editText  = (EditText) findViewById(R.id.addd);
    editText  = (EditText) findViewById(R.id.description);

      Calendar c=Calendar.getInstance();
        mYear=c.get(Calendar.YEAR);
         mMonth=c.get(Calendar.MONTH);
         mDay=c.get(Calendar.DAY_OF_MONTH);
        hour=c.get(Calendar.HOUR);
        min=c.get(Calendar.MINUTE);

       dateformat();
       timeformat();
    }   

 private void dateformat() {
     // TODO Auto-generated method stub
    Calendar c=Calendar.getInstance();
     //String dateFormat = "dd/MM/yyyy";
         editText = (EditText) findViewById(R.id.text);
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
       editText.setText( sdf.format(c.getTime()));
       editText.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
           // TODO Auto-generated method stub
           showDialog(DATE_DIALOG_ID);
       }
   });
}

 private void timeformat() {
    // TODO Auto-generated method stub
     Calendar c=Calendar.getInstance();
     //string timeFormat = "HH:mm";
   editText =(EditText)findViewById(R.id.time);
    SimpleDateFormat stf = new SimpleDateFormat("HH:mm");
   editText.setText( stf.format(c.getTime()));  
   editText.setOnClickListener(new OnClickListener() {

        public void onClick(View v1) {
             // TODO Auto-generated method stub
             showDialog(TIME_DIALOG_ID);
          }
     });    
}

protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
    return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);

case TIME_DIALOG_ID:
    return new TimePickerDialog(this, timeSetListener, hour, min, false);

}

return null;
  }

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

public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {

    mYear = year;
    mMonth = monthOfYear;
    mDay = dayOfMonth;
    editText.setText(new     StringBuilder().append(mDay).append("/").append(mMonth+1).append("/").append(mYear));

}
 };

private TimePickerDialog.OnTimeSetListener timeSetListener=new  TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // TODO Auto-generated method stub

    hour=hourOfDay;
    min=minute;

editText.setText(new StringBuilder().append(hour).append(":").append(min));
}
};

//public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
//return true;
//}
}
4

1 回答 1

0

老兄不要尝试使用日期选择器或时间选择器重新发明轮子:这里有一些链接:

http://www.mkyong.com/android/android-date-picker-example/

http://www.mkyong.com/android/android-time-picker-example/

干杯,

于 2013-05-31T13:43:51.980 回答