开发者

How do I avoid hiding (or dismissing) an AlertDialog when clicking a NeutralButton?

I'm trying to use a multiple selectable dialog as the code below. And I want to use a neutral button to deselect all the items in the list. But when clicking a button wh开发者_JAVA百科ichever on the dialog, the dialog disappears immediately, I assume it must be a default action. But I want to remain it since a user doesn't expect that action I think. Is it possible to avoid disappearing a dialog when clicking a button on it, or should I make 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


If you add a button using any of those setButton methods the dialog will dismiss after the handler runs. So you have two options. Create your own dialog and have the button work however you want. Or two, Since you are using a list you may be able to call getListView on the AlertDialog and call addFooterView with a Button. That solution may look weird though with the other buttons.


I achieved selecting/deselecting all the items by the following approach. The idea comes from Rpond's advice, thanks 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>

-- Code here

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;
}

It works exactly what I want to. yokyo


You might be better off creating a custom dialog. You can use any activity, and just set the theme for the activity to Theme.Dialog. I.e, in your manifest:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜