[英]AlertDialog return boolean value
我試圖在提示輸入密碼的方法中包含一個AlertDialog構建器,當按下肯定按鈕時,根據數據庫值檢查它,並向方法調用者返回true或false值。
例如:添加/編輯/刪除用戶任務需要密碼。 我不想為這三個(或更多)這些操作生成不同的AlertDialog。 我想將以下代碼包裝在我可以從任何活動調用的TaskService類中,並根據該活動中的結果做出反應。
所以TaskService.java會有:
public boolean isCorrectPin(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
final EditText editText = new EditText(context);
builder.setView(editText);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editText.getText().toString()) == getPinCode(){
//return true
}
}
});
builder.show();
}
和OpenTaskAdapter.java將具有:
public void onBindViewHolder(ViewHolder holder, int position){
holder.btnMarkAsComplete.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (service.isCorrectPin(v) {
//complete task
}
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (service.isCorrectPin(v) {
//delete task
}
}
});
值得注意的是,這兩個按鈕監聽器可能處於完全不同的活動中。
對話框不能以您期望的方式“返回”值。 一個對話框可以對其他對象進行更改,但是您不能在其上有一些代碼塊並等待用戶完成與它的交互。
相反,您需要設置偵聽器,以便在解除提示對話框或按鈕或單擊時,或者您擁有所需的任何其他事件信號。 然后,這些偵聽器可以讀取對話框收集和設置的數據。
您可以創建自己的方法來生成與偵聽器的對話:
public void isCorrectPin(Context context, String title, String message, String btnPositive, final DialogSingleButtonListener dialogSingleButtonListener) {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setTitle(title);
dialogBuilder.setMessage(message);
dialogBuilder.setPositiveButton(btnPositive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editText.getText().toString() == getPinCode()){
dialogSingleButtonListener.onButtonClicked(dialog);
}
}
});
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
和聽眾類:
public interface DialogSingleButtonListener {
public abstract void onButtonClicked(DialogInterface dialog);
}
並使用它像:
service.isCorrectPin(context, title, message, btnPositive
new DialogSingleButtonListener() {
@Override
public void onButtonClicked(DialogInterface dialog) {
//code here is only called if they entered a correct pin.
}
}
);
這就是我在做的事情:
public Boolean showAlert(String message)
{
action = false;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(HAActivity.this);
// Setting Dialog Title
alertDialog.setTitle(getString(R.string.app_name));
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting Icon to Dialog
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
action = true;
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
action = false;
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
return action;
}
並調用這樣的函數://在其中創建函數的活動
if (Activity.showAlert("Do you really want to delete ??"))
{
//delete it anyway.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.