gpt4 book ai didi

java - 如何立即刷新AlertDialog的内容

转载 作者:行者123 更新时间:2023-12-02 11:41:50 25 4
gpt4 key购买 nike

这是我的问题。我有一个按钮,如果您当前没有选择日期,则该按钮将被禁用。我的问题是每次我选择日期时,我的程序中的条件都不起作用。如果我选择了所需的日期,该按钮应该立即启用。但在我当前输入所需日期后,该按钮仍然禁用。仅当我按下 AlertDialog 窗口的外部,然后再次按下将加载警报对话框的按钮时,它才会起作用,该按钮将更新并启用该按钮。

AlertDialog with disabled button ,

AlertDialog when I choose my desired date ,

AlertDialog when I press the button that will show the AlertDialog

这是我的程序的一部分:

private void Button(final String bookTitle, final String id) {
mBorrowed = "not borrowed";

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.borrow_dialog, (ViewGroup) findViewById(R.id.linearLayout));

dialogBuilder.setView(view);


tvScheduleDate = (TextView) view.findViewById(R.id.tvScheduleDate);
tvReturnDate = (TextView) view.findViewById(R.id.tvReturnDate);
tvSchedule = (TextView) view.findViewById(R.id.tvSchedule);
btnRequest = (Button) view.findViewById(R.id.btnRequest);

final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
tvSchedule.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


Calendar c = Calendar.getInstance();

year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(Borrow.this, Borrow.this, year, month, day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.add(Calendar.DAY_OF_WEEK, 6); //for max date
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
datePickerDialog.show();


}
});

btnRequest.setEnabled(false);
if (dayFinal > 0) {
btnRequest.setEnabled(true);

btnRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final FirebaseUser user = firebaseAuth.getCurrentUser();
/*

String format_date = yearFinal + "-" + monthFinal + "-" + dayFinal ;
String ids = databaseBorrow.push().getKey();
String buttonText = btnBorrow.getText().toString();
BorrowBook borrow = new BorrowBook(ids,buttonText,bookTitle,getPresentDate(format_date),getFutureDate(format_date));
databaseBorrow.child(ids).setValue(borrow);
Toast.makeText(getApplicationContext(),"Borrow Successfully",Toast.LENGTH_SHORT).show();*/
btnRequest.setEnabled(false);
if (mBorrowed.equals("not borrowed")) {
databaseBorrow.child("Borrow").child(user.getUid()).child(id).child("request_type").setValue("sent").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
databaseBorrow.child("Borrow").child(id).child(user.getUid()).child("request_type").setValue("received").addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
btnRequest.setEnabled(true);
mBorrowed = "req_sent";
btnRequest.setText("Cancel Borrowed Request");
//Toast.makeText(Borrow.this,"Request Sent Successfully"+mBorrowed,Toast.LENGTH_SHORT);
}
});
} else {
Toast.makeText(Borrow.this, "Failed request", Toast.LENGTH_SHORT);
}
}
});
}
if (mBorrowed.equals("req_sent")) {
databaseBorrow.child("Borrow").child(user.getUid()).child(id).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
databaseBorrow.child("Borrow").child(id).child(user.getUid()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
btnRequest.setEnabled(true);
mBorrowed = "not borrowed";
btnRequest.setText("Send Borrowed Request");
}
});
}
});
}
}
});
}
}

最佳答案

DatePickerDialog上设置

像这样:

DatePickerDialog datePickerDialog = new DatePickerDialog(Borrow.this, Borrow.this, year, month, day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.add(Calendar.DAY_OF_WEEK, 6); //for max date
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btnRequest.setEnabled(true);
}
});
datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btnRequest.setEnabled(false);
}
});
datePickerDialog.show();

您可以使用DatePicker而不是使用DatapickerDialogue

像这样:

final DatePicker datePicker = new DatePicker(mActivity);
datePicker.setMinDate(Calendar.getInstance().getTimeInMillis());
new AlertDialog.Builder(mActivity)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Picker", datePicker.getDayOfMonth() + " : " + datePicker.getMonth() + " : " + datePicker.getYear());

btnRequest.setEnabled(true);
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
btnRequest.setEnabled(false);
}
}).setView(datePicker).show();

关于java - 如何立即刷新AlertDialog的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48485881/

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