Android - How an AlertDialog injected with spinner can be closed when touched outer view?

老子叫甜甜 提交于 2019-12-13 21:08:19

问题


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.



来源:https://stackoverflow.com/questions/26961287/android-how-an-alertdialog-injected-with-spinner-can-be-closed-when-touched-ou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!