简体   繁体   中英

Both of my alert dialogs are showing when I press a button for one in my Android Activity

I have an activity with a menu inflator. There are two items which each should open there own dialog box. However when I press one both dialogs are opened on top of each other.

One of the two of the below classes are used depending on the button pressed. This is the execution line in the menu switch/case

case R.id.action_vote:
            VoteDialog.voteDialogShow(this);            

        case R.id.action_admin:
            AdminDialog.adminDialogShow(this);

These are the two dialog classes.. I removed some of the code for readability.

public class AdminDialog {

    public static void adminDialogShow(final Context context){

        final AlertDialog.Builder adminAlert = new AlertDialog.Builder(context);

        adminAlert.setTitle("Restricted Area!");
        adminAlert.setMessage("Enter Access Code:");

        adminAlert.setView(input);
        //....
        //Setup text and buttons......
        //....
        adminAlert.show();
    }
}

public class VoteDialog {

    public static void voteDialogShow(final Context context){

        final AlertDialog.Builder voteAlert = new AlertDialog.Builder(context);

        //....
        //Setup text and buttons......
        //....

        voteAlert.setView(input);

        //....

        voteAlert.show();
    }
}

Change to:

case R.id.action_vote:
    VoteDialog.voteDialogShow(this);    
    break;        

case R.id.action_admin:
    AdminDialog.adminDialogShow(this);
    break;

As pointed out by Mike M., you're missing the break; for each case in your switch to mark where its efects end. More details at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM