我已经在我的活动上实现了日期选择器对话框,它运行正常。当我点击取消时,取消选择器被解雇,因此按钮工作。但是我想捕获取消按钮事件并添加更多代码来完成活动,但我似乎无法处理它。 这是我的代码:
require("http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907");

我试过的是添加这个onclick方法,如:
public class DatePickerActivity extends Activity {
static final int DATE_PICKER_ID = 1111;
private int year;
private int month;
private int day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_PICKER_ID);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker
return new DatePickerDialog(this, pickerListener, year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
Log.d("Date selected", String.valueOf(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" ")));
Intent intent = new Intent();
intent.putExtra("year", String.valueOf(new StringBuilder().append(year )));
intent.putExtra("month", String.valueOf(new StringBuilder().append(month + 1)));
intent.putExtra("day", String.valueOf(new StringBuilder().append(day)));
setResult(RESULT_OK, intent);
finish();
}
};
}

但这不起作用。有什么建议吗?
答案 0 :(得分:4)
为DatePickerDialog创建一个类,并从onCreate()
调用它,如下所示:
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);
// Return the DatePickerDialog
return dpd;
}
public void onDateSet(DatePicker view, int year, int month, int day){
// Your code here
}
// Set a Listener for Dialog Cancel button click event
/*
onCancel(DialogInterface dialog)
This method will be invoked when the dialog is canceled.
*/
public void onCancel(DialogInterface dialog){
// Send a message to confirm cancel button click
Toast.makeText(getActivity(),"Date Picker Canceled.", Toast.LENGTH_SHORT).show();
}
}
从onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
DialogFragment dFragment = new DatePickerFragment();
// Show the date picker dialog fragment
dFragment.show(getFragmentManager(), "Date Picker");
}
答案 1 :(得分:1)
您可以尝试此代码
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker
DatePickerDialog dialog = new DatePickerDialog(this, pickerListener, year, month,day);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Text", new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
// Do Stuff
Log.i("dialog click", "dialog negative button clicked");
dialog.dismiss();
}
}
});
return dialog;
}
return null;
}