gpt4 book ai didi

Android DatePickerDialog 取消按钮不起作用

转载 作者:行者123 更新时间:2023-11-29 00:23:25 24 4
gpt4 key购买 nike

我正在使用 DatePicker 对话框。当一个按钮被点击时 datepicker show一切正常。

但是我的 BUTTON_NEGATIVE 按钮不起作用。我点击了 NEGATIVE_BUTTON,diloagpicker 关闭但日期已设置。

这是我的 CalendarItem.java 文件:

    public class CalendarItem extends DialogFragment implements DatePickerDialog.OnDateSetListener {

private Button pregnant_button;
private Button numbered_button;

public CalendarItem(Button pregnant_button) {
this.pregnant_button = pregnant_button;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

/*
tarih önceden ayarlanmışsa,
*/
long get_miliseconds_to_date = MotherActivity.preferences.getLong(MotherActivity.USER_DUE_DATE_MS,0);
final Calendar c = Calendar.getInstance();
if(get_miliseconds_to_date != 0 ) {
c.setTimeInMillis(get_miliseconds_to_date);
}
// Use the current date as the default date in the picker
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);

dialog.setButton(
DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.dismiss();
Log.d("Button_value","" + which + " ---- " + " negatif");
}
}
});

dialog.setButton(
DialogInterface.BUTTON_POSITIVE,"Set",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Button_value","" + which + " ---- " + " Pozitif");
}
}
);

// Create a new instance of DatePickerDialog and return it
return dialog;
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
pregnant_button.setText(dayOfMonth + " " + MotherActivity.turkish_monts[monthOfYear] + " " + year);
long miliseconds_date = date_to_miliseconds(year, monthOfYear, dayOfMonth);

SharedPreferences.Editor editor = MotherActivity.preferences.edit();
editor.putLong(MotherActivity.USER_DUE_DATE_MS, miliseconds_date);
editor.apply();
}

private long date_to_miliseconds(int year, int month, int day){
Calendar calendar = Calendar.getInstance();

calendar.set(year,month,day,00,00);
return calendar.getTimeInMillis();
}
}

帮帮我。谢谢。

最佳答案

我很容易地解决了我的问题。

除了 bool 变量,我只添加了一行代码。

  private Button pregnant_button;
//add this line default = false
private boolean printer = false;

public CalendarItem(Button pregnant_button) {
this.pregnant_button = pregnant_button;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

/*
tarih önceden ayarlanmışsa,
*/
long get_miliseconds_to_date = MotherActivity.preferences.getLong(MotherActivity.USER_DUE_DATE_MS,0);
final Calendar c = Calendar.getInstance();
if(get_miliseconds_to_date != 0 ) {
c.setTimeInMillis(get_miliseconds_to_date);
}
// Use the current date as the default date in the picker
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);


dialog.setButton(
DialogInterface.BUTTON_NEGATIVE, "İptal",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {

}
}
});
dialog.setButton(
DialogInterface.BUTTON_POSITIVE, "Tamam",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
printer = true;
}
}
});

// Create a new instance of DatePickerDialog and return it
return dialog;
}

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

if( printer ) {
pregnant_button.setText(dayOfMonth + " " + MotherActivity.turkish_monts[monthOfYear] + " " + year);
long miliseconds_date = date_to_miliseconds(year, monthOfYear, dayOfMonth);
SharedPreferences.Editor editor = MotherActivity.preferences.edit();
editor.putLong(MotherActivity.USER_DUE_DATE_MS, miliseconds_date);
editor.apply();
}
}

关于Android DatePickerDialog 取消按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21527128/

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