这是我的问题。如果您当前没有选择日期,我有一个禁用的按钮。我的问题是每次我选择日期,我的程序中的条件不起作用。如果我选择了我想要的日期,应该立即启用该按钮。但是在我目前投入所需日期后,该按钮仍处于停用状态。只有当我按下AlertDialog窗口的外部时,它才会起作用,再次按下将加载Alert对话框的按钮,该按钮将更新并且按钮已启用。
,
,
以下是我的计划部分:
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");
}
});
}
});
}
}
});
}
}
答案 0 :(得分:1)
在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();
OR
您可以使用 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();