这是我的活动
public class PetRegistrationActivity extends Activity {
private Button born;
private Button take;
private Date date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pet_registration);
final Button btnSave = (Button) findViewById(R.id.save_info_button);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
performSave();
}
});
}
private void performSave() {
Pet p = new Pet();
date = new Date(p,R.id.generalinfo_datepicker_dob,
R.id.generalinfo_datepicker_takeover,
this.getWindow().getDecorView().findViewById(android.R.id.content),
PetRegistrationActivity.this.getApplicationContext());
born = (Button) findViewById(R.id.generalinfo_datepicker_dob);
take = (Button) findViewById(R.id.generalinfo_datepicker_takeover);
date.buildPicker(p, R.id.generalinfo_datepicker_dob,
R.id.generalinfo_datepicker_takeover);
date.buildPicker(p, R.id.generalinfo_datepicker_takeover,
R.id.generalinfo_datepicker_takeover);
DataHelper.getInstance().storeImage(this.selectedImg,
getApplicationContext(), String.valueOf(p.getId()));
final Intent resultIntent = new Intent(PetRegistrationActivity.this,MainActivity.class);
resultIntent.putExtra(Constants.NEW_PET, p);
setResult(Activity.RESULT_OK, resultIntent);
this.finish();
}
}
这是Class Date的代码:
public class Date {
private final IPickerHelper picker;
private final int id;
private final int secondID;
private final View v;
private final Context ctx;
private Button dateButton;
private SimpleDateFormat fmt;
public Date(IPickerHelper picker, int id, int secondID,View v,Context ctx){
this.picker = picker;
this.id = id;
this.secondID = secondID;
this.v = v;
this.ctx = ctx;
}
public void buildPicker(final IPickerHelper pickerObj,
final int buttonId, final int secondButtonId) {
dateButton = (Button) v.findViewById(buttonId);
fmt = new SimpleDateFormat("dd/MM/yyyy", Locale.ITALIAN);
if (pickerObj.getStartDate() == null) {
final java.util.Date date = new java.util.Date();
pickerObj.setStartDate(date);
pickerObj.setEndDate(date);
dateButton.setText(fmt.format(date));
} else {
dateButton.setText(fmt.format(pickerObj.getStartDate()));
final Button endingButton = (Button) v.findViewById(secondButtonId);
endingButton.setText(fmt.format(pickerObj.getEndDate()));
}
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
final java.util.Date value = new java.util.Date();
final Calendar cal = Calendar.getInstance();
cal.setTime(value);
if (secondButtonId == -1) {
setTimePickerDialog(pickerObj, buttonId);
} else {
setDatePickerDialog(pickerObj, buttonId, cal, fmt);
}
}
});
}
private void setDatePickerDialog(final IPickerHelper pickerObj, final int buttonId,
final Calendar cal, final SimpleDateFormat fmt) {
new DatePickerDialog(ctx, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(final DatePicker view, final int y, final int m, final int d) {
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m);
cal.set(Calendar.DAY_OF_MONTH, d);
final Button dateButton = (Button) v.findViewById(buttonId);
if (buttonId == R.id.generalinfo_datepicker_dob) {
pickerObj.setStartDate(cal.getTime());
final Button endingButton = (Button) v.findViewById(R.id.generalinfo_datepicker_takeover);
endingButton.setText(fmt.format(cal.getTime()));
} else {
pickerObj.setEndDate(cal.getTime());
}
dateButton.setText(fmt.format(cal.getTime()));
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal
.get(Calendar.DAY_OF_MONTH)).show();
}
}
点击它们时,两个按钮(生成和拍摄)都不执行任何操作。
答案 0 :(得分:2)
将以下内容放入onCreate
:
born = (Button) findViewById(R.id.generalinfo_datepicker_dob);
take = (Button) findViewById(R.id.generalinfo_datepicker_takeover);
答案 1 :(得分:1)
将这两行移到onCreate
内,因为@Bouterbiat已经注意到了。但也不要忘记为这两个按钮添加听众,这是您在代码中没有做过的事情:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pet_registration);
final Button btnSave = (Button) findViewById(R.id.save_info_button);
born = (Button) findViewById(R.id.generalinfo_datepicker_dob);
take = (Button) findViewById(R.id.generalinfo_datepicker_takeover);
born.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
//Do whatever born does
}
});
take.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
//Do whatever take does
}
});