How to prevent a dialog from closing when a button is clicked

后端 未结 18 1955
无人及你
无人及你 2020-11-21 23:59

I have a dialog with EditText for input. When I click the \"yes\" button on dialog, it will validate the input and then close the dialog. However, if the input

18条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 00:25

    public class ComentarDialog extends DialogFragment{
    private EditText comentario;
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View v = inflater.inflate(R.layout.dialog_comentar, null);
        comentario = (EditText)v.findViewById(R.id.etxt_comentar_dialog);
    
        builder.setTitle("Comentar")
               .setView(v)
               .setPositiveButton("OK", null)
               .setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
    
                   }
               });
    
        return builder.create();
    }
    
    @Override
    public void onStart() {
        super.onStart();
    
        //Obtenemos el AlertDialog
        AlertDialog dialog = (AlertDialog)getDialog();
    
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);//Al presionar atras no desaparece
    
        //Implementamos el listener del boton OK para mostrar el toast
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(comentario.getText())){
                   Toast.makeText(getActivity(), "Ingrese un comentario", Toast.LENGTH_SHORT).show();
                   return;
                }
                else{
                    ((AlertDialog)getDialog()).dismiss();
                }
            }
        });
    
        //Personalizamos
        Resources res = getResources();
    
        //Buttons
        Button positive_button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        positive_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));
    
        Button negative_button =  dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        negative_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));
    
        int color = Color.parseColor("#304f5a");
    
        //Title
        int titleId = res.getIdentifier("alertTitle", "id", "android");
        View title = dialog.findViewById(titleId);
        if (title != null) {
            ((TextView) title).setTextColor(color);
        }
    
        //Title divider
        int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
        View titleDivider = dialog.findViewById(titleDividerId);
        if (titleDivider != null) {
            titleDivider.setBackgroundColor(res.getColor(R.color.list_menu_divider));
        }
    }
    }
    

提交回复
热议问题