I have problem in declaring the intents on my app. I have a form with text fields and spinner. I have added a button which onClick must display Datepicker. I have declared t开发者_如何学编程he DatePicker as a new class and added to intent onCLick.
date.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
Intent i=new Intent(SendMail.this,DatePicker.class);
startActivity(i);
}
});
In manifest i have declared
<activity android:name=".DatePicker">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now the problem is when i click the button the whole form is getting reset and all the spinner values are getting vanished. This must be because of declaring the intent wrongly. So, can anyone specify how to declare my intent so that the DatePicker will be called on the main form iteself
You don't need a new activity for a date picker result.
Make your SendMail
class to have these components...
public class SendMail extends Activity {
static final int DATE_DIALOG_ID = 1;
// date and time
private int mYear;
private int mMonth;
private int mDay;
public void launchSetDate() {
showDialog(DATE_DIALOG_ID);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
// do something with the result
}
};
}
Then instead of running startActivity, you call a method launchSetDate()
;
精彩评论