如何处理自定义alertDialog的外部单击?

时间:2018-06-20 12:28:19

标签: android alertdialog

实际上,在我的应用程序中,当我滑动RecyclerView时,如果我正在使用按钮进行操作,它将打开一个自定义AlertDialog。

当我在AlertDialog上按下时,问题就来了,因为它关闭了AlertDialog,而没有将通知发送给recyclerView,因此recyclerView不会从swipeMode退回。

如何处理AlertDialog之外的单击?我只需要在此句柄中添加exampleAdapter.notifyItemChanged。

这是我的AlertDialog代码:

public void customAllertQuantity(final int position){

    AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);

    @SuppressLint("InflateParams") View mView = getLayoutInflater().inflate(R.layout.alert_quantity, null);

    final EditText quantita = mView.findViewById(R.id.editTextQTA);

    Button buttonPiu = mView.findViewById(R.id.btnPLUS);
    Button buttonMeno = mView.findViewById(R.id.btnMINUS);
    ImageButton save = mView.findViewById(R.id.saveButton);

    quantita.setText(String.valueOf(itemCassas.get(position).getQuant()));

    mBuilder.setView(mView);
    final AlertDialog dialog = mBuilder.create();
    dialog.show();

    buttonPiu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            quantita.setText(String.valueOf((Integer.parseInt(quantita.getText().toString()) + 1)));
        }
    });

    buttonMeno.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(Integer.parseInt(quantita.getText().toString()) > 1){
                quantita.setText(String.valueOf((Integer.parseInt(quantita.getText().toString()) - 1)));
            }else{
                Log.i("LOG","QTA = 1");
                //
            }
        }
    });

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double prezz = itemCassas.get(position).getImporto();
            itemCassas.get(position).setQuant(Integer.parseInt(quantita.getText().toString()));
            itemCassas.get(position).setPrice(prezz *  itemCassas.get(position).getQuant());
            TotalPrice();
            exampleAdapter.notifyItemChanged(position);
            dialog.dismiss();

        }
    });


}

1 个答案:

答案 0 :(得分:3)

有两种方法,禁用取消或处理对话框取消(“在对话框外单击”)。

您可以避免对话框被取消,如下所示:

mBuilder.setView(mView);
mbuilder.setCancelable(false);
final AlertDialog dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);

处理取消动作或“在对话框外单击”:

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            //send notification to adapter
        }
    });

注意:该对话框可能由于取消或选择提供的选项之一之外的其他原因而被关闭。如果您希望听取所有关闭对话框的情况,请使用setOnDismissListener