Android - How an AlertDialog injected with spinner

2019-08-28 00:52发布

问题:

Basically what I want to do is closing Spinner's Dialog Box and the Customized Dialog I've created when clicked outside of those boxes. Anyone knows how to handle these?

dialog.xml, My Spinner's xml, MakeandShowDialogBox function below :

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout 
        android:id="@+id/scheduleTable"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        ></TableLayout>

</LinearLayout>

Spinner XML

<Spinner
    android:id="@+id/spinList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/strDepartmentNames"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:spinnerMode="dialog"
    android:paddingTop="15dp"
    android:paddingBottom="15dp" />

makeAndShowDialogBox

private void makeAndShowDialogBox() {

    AlertDialog.Builder myDialogBox = new AlertDialog.Builder(this);

    final LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final View dialogView = layoutInflater.inflate(R.layout.dialog, null);

    // Set three option buttons
    myDialogBox.setPositiveButton("Close",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });

    myDialogBox.setView(dialogView);
    myDialogBox.create();
    myDialogBox.show();
}

回答1:

you just add this option in your dialog

myDialogBox.setCancelable(true);



回答2:

Did you try to call this method setCanceledOnTouchOutside(true).

myDialogBox.setCanceledOnTouchOutside(true);

Afaik It should work.