开发者

Is it possible to make a button on an AlertDialog that doesn't automatically close the dialog?

I have a simple list view with some check boxes in an alert dialog. I need to option to add a select all/none but you can't bring up the menu in an alert dialog, and I want to do this function from a button. From 开发者_JS百科what I've seen any kind of button (positive, neutral, and negative) all close the dialog regardless.

So, is this possible? If no, what alternatives do I have? My last mitigation is to simply create a new view and recreate everything. Is a new view the best solution?


So, is this possible?

Not from the standard buttons in AlertDialog, AFAIK.

If no, what alternatives do I have?

Here are some:

  1. Don't offer select all/none
  2. Don't use AlertDialog, but use a dialog-themed activity
  3. Don't use AlertDialog, but use some other subclass of Dialog
  4. Your cited option (don't use setAdapter(), but rather create your own View for the dialog contents)
  5. Don't use a Dialog at all, but a ListActivity started via startActivityForResult() (akin to #2, just without worrying about the theme)


You have to overide the onClickListener of the button. For example for the Neutral Button, you will have something like this:

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
        * 
        */

    }
});


So, is this possible?

You may try to use the following method to place your custom view on AlertDialog:

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

That view can contain your button. Just attach an onClickListener to the button that would operate with your list view.


Have you tried to extend the AlertDialog class and have it implment 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.
        }
    }
}

Following this template, you can put any buttons you need and create your own functionality within the dialog itself. You could also create your own Button at runtime, but you'll have to do the extra work of configuring the button's text, size, icon, etc.

You can then create the dialog in your activity under the onCreateDialog() call.

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

Hope this helps.


It is possible. The solution is

  1. Override the dismiss method to do nothing. This will prevent the dialog from being dismiss when any of the button is clicked. Optionally, you can also save the original dismiss.

  2. Modify your OnClickListener so that it calls the superclass of your dialog (i.e. AlertDialog) 's dismiss() (namely super.dismiss()) when the button(s) you want is(are) clicked.

I usually make the Dialog class the listener so I will do something like this

public class MyAlertDialog extends AlertDialog implements OnClickListener {

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

    }

    // superclass's 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 it will only dismiss the dialog when either the button NEGATIVE or POSITIVE is clicked but keep the dialog on display when the button NEUTRAL is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜