The use of android-spinner drop-down box

1. Add the spinner mark in the xml file
Insert picture description here
2. Get the control in the java file, and get some data at the same time
Insert picture description here
3. Come to an arrayadapter adapter, bind the adapter
Insert picture description here
4. Add monitoring events
Insert picture description here
5. The total code

spinner1 = this.findViewById(R.id.spinner);
        List<String> list = new ArrayList<>();
        list.add("体育");
        list.add("音乐");
        list.add("文学");
        list.add("礼仪");
        ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, list);
        spinner1.setAdapter(adapter);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
    
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
    
                switch (position){
    
    
                    case 0:
                        Toast.makeText(MainActivity.this,"你点击了体育",Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(MainActivity.this,"你点击了音乐",Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this,"你点击了文学",Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this,"你点击了礼仪",Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
    

            }
        });

effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/Willow_Spring/article/details/112869401