How to avoid hiding (or rejecting) an AlertDialog when a NeutralButton button is clicked?

I am trying to use several selectable dialog boxes as the code below. And I want to use the neutral button to deselect all the items in the list. But when you click a button in a dialog box, the dialog disappears immediately, I assume that this should be the default action. But I want to stay, as the user does not expect such an action, which I think. Is it possible to avoid the disappearance of the dialog when you click a button on it or to create a custom dialog?

protected Dialog onCreateDialog( int index) 
{
    return new AlertDialog.Builder(this)
            .setTitle( "title" )
            .setMultiChoiceItems(items, selections, new DialogInterface.OnMultiChoiceClickListener(){
                @Override   
                public void onClick( DialogInterface dialog, int clicked, boolean selected ) { }    
            })      
            .setPositiveButton( "OK", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int id)
                {
                    //Do something
                }
            })
            .setNeutralButton( "Deselect all", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int id)
                {
                    //Do something
                }
            })
            .create();
}

Thanks in advance, yokyo

+3
source share
3 answers

/ . Rpond, Rpond!

-/res/layout/dialog_footer.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footerRoot"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button     android:id="@+id/selectAllButton"
    android:text="@string/select_all"
    style="@style/FooterButton" />
</LinearLayout>

-

protected Dialog onCreateDialog( int index) 
{
    AlertDialog builder= new AlertDialog.Builder(this)
            .setTitle( "title" )
            .setMultiChoiceItems(items, selections, new DialogInterface.OnMultiChoiceClickListener(){
                @Override   
                public void onClick( DialogInterface dialog, int clicked, boolean selected ) { }    
            })      
            .create();

LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_footer, (ViewGroup)findViewById(R.id.footerRoot));

    Button selectAllButton = (Button)layout.findViewById(R.id.selectAllButton);
    selectAllButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            Log.v("Test", "hello");
        }           
    });     

    builder.setView(layout);
    return builder;
}

, . yokyo

+3

setButton, . , . , . . , getListView AlertDialog addFooterView . , .

+3

, . Theme.Dialog. I.e, :

<activity android:theme="@android:style/Theme.Dialog">
+2

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


All Articles