按下按钮时,Fragment中的AlertDialog未关闭

时间:2014-10-16 10:56:59

标签: android android-fragments android-alertdialog

我有片段,我正在使用AlertDialog。在关闭Activity中的AlertDialog时没有问题但是当我在Fragment中使用相同的代码时,AlertDialog不会关闭直到我按下按钮最少3次

在按下Ok按钮后,alertDialog冻结2秒,然后显示Toast msg。但是对话框没有关闭。

我也尝试过使用dialog.dismiss()。但问题仍然存在。

这是我的CheckFragment类。

public class ChecckFragment extends Fragment implements OnItemSelectedListener {
    private static AlertDialog alertDialog;
    private Context context
     @Override 
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         context = this.getActivity();     
       }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     //other codes....
     View btnCheck = (Button) rootView.findViewById(R.id.btnSaveNew);
     btnCheck.setOnClickListener(listener);
      }
   private OnClickListener listener = new OnClickListener() {

    @Override
    public void onClick(View v) {
         createCheckBox();
         alertDialog.show();
         }
    }
   //Function to create checkBox.
   private void createCheckBox(){
    final CharSequence[] checkSequences = {" Low "," Mid "," High "," Very High"};
    final ArrayList<Integer> itemList = new ArrayList<>();

    AlertDialog.Builder checkBuilder= new AlertDialog.Builder(context);
    checkBuilder.setTitle("Select School Leaving Reason");
    checkBuilder.setMultiChoiceItems(checkSequences, null, new         DialogInterface.OnMultiChoiceClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
            if (isChecked) {
                itemList.add(indexSelected);
            } else if (itemList.contains(indexSelected)) {
                itemList.remove(Integer.valueOf(indexSelected));
            }
        }
    });

    checkBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            StringBuilder sb = new StringBuilder();
            for(int str : itemList){
             sb.append(",");    //separating contents using semi colon
                sb.append(str); 
            }
            sb.deleteCharAt(0); //Delete chatacter at 0.
            String string = sb.toString();
         Toast.makeText(context, "Selected Items:"+ sb, Toast.LENGTH_LONG).show();
        alertDialog.dismiss();//Called dismiss here but dialog doesnt closes
        }
    });
    checkBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();//Called dismiss here too but dialog doesnt closes
        }
    });

    alertDialog = checkBuilder.create();

}
}

我真的被男人困扰了。请帮助!!!!

6 个答案:

答案 0 :(得分:2)

如果您正在使用片段DialogFragment,则建议使用最佳方法。

但是如果您想坚持使用警报对话框,则不应在createCheckBox()课程中声明Fragment,因为getActivity有时可能会冻结您的片段,因此alertDialog是没有被解雇。 要解决此问题,请在createCheckBox()课程中声明您的FragmentActivity方法,如下所示:

private void createCheckBox(){ 
    final CharSequence[] checkSequences = {" Low "," Mid "," High "," Very High"};
    final ArrayList<Integer> itemList = new ArrayList<>();

    AlertDialog.Builder checkBuilder= new AlertDialog.Builder(context);
    checkBuilder.setTitle("Select School Leaving Reason");
    checkBuilder.setMultiChoiceItems(checkSequences, null, new         DialogInterface.OnMultiChoiceClickListener() {

        @Override 
        public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
            if (isChecked) {
                itemList.add(indexSelected);
            } else if (itemList.contains(indexSelected)) {
                itemList.remove(Integer.valueOf(indexSelected));
            } 
        } 
    }); 

    checkBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override 
        public void onClick(DialogInterface dialog, int which) {
            StringBuilder sb = new StringBuilder();
            for(int str : itemList){
             sb.append(",");    //separating contents using semi colon
                sb.append(str); 
            } 
            sb.deleteCharAt(0); //Delete chatacter at 0.
            String string = sb.toString();
         Toast.makeText(context, "Selected Items:"+ sb, Toast.LENGTH_LONG).show();
        alertDialog.dismiss();//Called dismiss here but dialog doesnt closes
        } 
    }); 
    checkBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override 
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();//Called dismiss here too but dialog doesnt closes
        } 
    }); 

    alertDialog = checkBuilder.create();
    alertDialog.show();
}
//*************************Method for  selecting dialog*******************************//

    protected Dialog onCreateDialog(int id) {
            switch (id) {
            case ALERT_DIALOG:
                createCheckBox(0);
                return alertDialog;         
            return null;
        }
    }   

现在从您的activity类调用createCheckBox()方法,如下所示:     开始(删除开始结束)

public class ChecckFragment extends Fragment implements OnItemSelectedListener { 
    private static AlertDialog alertDialog;
    private final int ALERT_DIALOG = 0;
    private Context context
     @Override  
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         context = this.getActivity();     
       } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     //other codes.... 
     View btnCheck = (Button) rootView.findViewById(R.id.btnSaveNew);
     btnCheck.setOnClickListener(listener);
      } 
   private OnClickListener listener = new OnClickListener() {

    @Override 
    public void onClick(View v) { 
         getActivity().showDialog(ALERT_DIALOG); //Calls your createMethod from activity class.
         } 
    }   
} 

这可能对您有所帮助。

答案 1 :(得分:0)

使用此代码

AlertDialog alertDialog= new AlertDialog.Builder(this).create(); 然后, 只需致电alertDialog.dismiss();

一定会有用。

答案 2 :(得分:0)

您已将alerDialogBu​​ilder初始化为checkBuilder并关闭AlertDialog

AlertDialog.Builder checkBuilder = new AlertDialog.Builder(context);

尝试关闭checkBuilder

checkBuilder.dismiss();

答案 3 :(得分:0)

你犯了一个愚蠢的错误。在尝试解雇DialogInterface

时,您需要取消AlertDialog

更改:

alertDialog.dismiss();

 dialog.dismiss(); 

其中dialog是onClick的参数 - &gt; public void onClick(DialogInterface dialog, int which)

答案 4 :(得分:0)

您需要在此处

之后将此代码alertDialog.show();移至createCheckBox()方法
 alertDialog = checkBuilder.create();
 alertDialog.show();

而不是

  private OnClickListener listener = new OnClickListener() {

  @Override
  public void onClick(View v) {
     createCheckBox();
     alertDialog.show();// remove from here
     }
   }

点击AlertDialog点击

的正面和负面按钮
 dialog.dismiss():

答案 5 :(得分:0)

AlertDialog.Builder checkBuilder;

public class ChecckFragment extends Fragment implements OnItemSelectedListener {
private static AlertDialog alertDialog;
private Context context
 @Override 
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     context = this.getActivity();     
   }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
 //other codes....
 View btnCheck = (Button) rootView.findViewById(R.id.btnSaveNew);
 btnCheck.setOnClickListener(listener);
  }
 private OnClickListener listener = new OnClickListener() {

@Override
public void onClick(View v) {
     createCheckBox();

     }
}
 //Function to create checkBox.
 private void createCheckBox(){
final CharSequence[] checkSequences = {" Low "," Mid "," High "," Very High"};
final ArrayList<Integer> itemList = new ArrayList<>();

 checkBuilder= new AlertDialog.Builder(context);
checkBuilder.setTitle("Select School Leaving Reason");
checkBuilder.setMultiChoiceItems(checkSequences, null, new               DialogInterface.OnMultiChoiceClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
        if (isChecked) {
            itemList.add(indexSelected);
        } else if (itemList.contains(indexSelected)) {
            itemList.remove(Integer.valueOf(indexSelected));
        }
    }
});

checkBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        StringBuilder sb = new StringBuilder();
        for(int str : itemList){
         sb.append(",");    //separating contents using semi colon
            sb.append(str); 
        }
        sb.deleteCharAt(0); //Delete chatacter at 0.
        String string = sb.toString();
     Toast.makeText(context, "Selected Items:"+ sb, Toast.LENGTH_LONG).show();
    dialog.dismiss();//Called dismiss here but dialog doesnt closes
    }
});
checkBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();  //<--------------------------------error found--
    }
});

alertDialog = checkBuilder.create();
alertDialog.show();

   }

}