简体   繁体   中英

Drop down menu on android button to start multiple activities

I want to create a button in my activity so that clicking the button shows drop down menu list and when an option is selected from drop down list a new activity start. So Basically I want to start multiple activities on my button click in android app. I have found this tutorial http://www.mkyong.com/android/android-spinner-drop-down-list-example/ but here i can just toast an item selected and if I try starting new intent instead of toast I get error. Kindly guide me plus if there is any better idea to do this let me know please.

You just need to modify the function that listens for clicks on the spinner, and start the desired activity depending on which item was clicked:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    Activity activity = (Activity) parent.getContext();
    Intent myIntent = new Intent();
    if (pos == 0) {
        myIntent.setClassName(activity, MyActivity1.class);
    } else if (pos == 1) {
        myIntent.setClassName(activity, MyActivity2.class);
    }
    activity.startActivity(myIntent);
}

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