Is it possible to create a button on AlertDialog that does not close the dialog box automatically?

I have a simple list view with some checkboxes in the warning dialog. I need to add the select all / none parameter, but you cannot open the menu in the warning dialog, and I want to make this function using the button. From what I saw, any button (positive, neutral and negative) closes the dialog independently.

So is this possible? If not, what are my alternatives? My last softening is to simply create a new view and recreate everything. Is a new look a better solution?

+3
source share
5 answers

, ?

AlertDialog, AFAIK.

, ?

:

  • select all/none
  • AlertDialog,
  • AlertDialog, Dialog
  • ( setAdapter(), View )
  • Dialog , ListActivity startActivityForResult() ( # 2, )
+6

onClickListener. , " " - :

AlertDialog dialog = builder.create();
dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Neutral", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    } 
});
dialog.setButton(AlertDialog.BUTTON_POSITIVE, "Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    } 
});
dialog.show();

Button neutralButton = dialog.getButton(DialogInterface.BUTTON_NEUTRAL);
neutralButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View onClick) {                 
        /**
        *
        * your code
        * 
        */

    }
});
+5

, ?

AlertDialog:

android.app.AlertDialog.Builder.setView(View view)

. onClickListener , .

0

AlertDialog OnCLickListener

public class MyDialog extends AlertDialog implements OnClickListener {
    private Button myButton;

    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        // use LayoutInflater to get at custom button
        LayoutInflater layoutInflater = (LayoutInflater)getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View contentView = layoutInflater.inflate( R.layout.mydialog_layout, null );

        // pull button from layout, set listener
        myButton = (Button)contentView.findViewById( R.id.myButtonId );
        myButton.setOnClickListener( this );

        setContentView( contentView );
    }

    public void onClick( View v ) {
        if ( v.getId() == R.id.myButtonId ) {
            // DO your button actions.
        }
    }
}

, . , , , ..

onCreateDialog().

protected Dialog onCreateDialog( int id ) {
    MyDialog dialog = new MyDialog( this, 0 );
    dialog.setOnCancelListener( this );
    dialog.setOnDismissListener( this );
    return dialog;
}

, .

0

.

  • dismiss . . .

  • OnClickListener , (, AlertDialog) () ( super.dismiss()), () .

Dialog , -

public class MyAlertDialog extends AlertDialog implements OnClickListener {

    // other methods
    @Override
    public void dismiss() {

    }

    // superclass dismiss, might come in handy when the OnClickListener is not this dialog
    public void normalDismiss() {
        super.dismiss();
    }

    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case BUTTON_NEGATIVE:
            // handle your event
            super.dismiss ();
            break;
        case BUTTON_NEUTRAL:
            // handle your event
            break;
        case BUTTON_POSITIVE:
        default:
            // handle your event
            super.dismiss ();
            break;
        }
    }

}

so that he rejects the dialogue only when the NEGATIVE or POSITIVE button is pressed, but keep the dialogue on the display when the NEUTRAL button.

0
source

Source: https://habr.com/ru/post/1740253/


All Articles