gpt4 book ai didi

android - 单击取消按钮上的日期选择器对话框时如何完成 Activity

转载 作者:太空狗 更新时间:2023-10-29 16:32:35 26 4
gpt4 key购买 nike

我已经在我的 Activity 中实现了日期选择器对话框,它工作正常。当我单击取消时,选择器被关闭,因此按钮可以工作。但是我想捕获取消按钮事件并向其添加更多代码以完成 Activity ,但我似乎无法处理它。这是我的代码:

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();

}
};
}

我尝试的是添加这个 onclick 方法,例如:

 public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

if(which==Dialog.BUTTON_NEGATIVE)
{
Log.i("dialog click", "dialog negative button clicked");
dialog.dismiss();
}

}

但这行不通。有什么建议吗?

最佳答案

为 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");
}

关于android - 单击取消按钮上的日期选择器对话框时如何完成 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35104181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com