安卓中使用下拉框(Spinner)



    
    


 

 

 

public class MainActivity extends AppCompatActivity {

    private Spinner mspinner;
private String zhye;
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] ctype = new String[]{"全部", "游戏", "电影", "娱乐", "图书"};
        //创建一个数组适配器
        adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, ctype);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     //设置下拉列表框的下拉选项样式

        mspinner = findViewById(R.id.spinner);
        mspinner.setAdapter(adapter);
//条目点击事件
        mspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            private String positions;

            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                positions = adapter.getItem(position);
                if (positions.equals("全部")){
                    zhye=2+"";

                    Toast.makeText(MainActivity.this, "全部", Toast.LENGTH_SHORT).show();
                }else if (positions.equals("游戏")){
                    zhye=3+"";
                    Toast.makeText(MainActivity.this, "游戏", Toast.LENGTH_SHORT).show();

                }else if (positions.equals("电影")){
                    zhye=4+"";
                    Toast.makeText(MainActivity.this, "电影", Toast.LENGTH_SHORT).show();

                }else if (positions.equals("娱乐")){
                    zhye=5+"";
                    Toast.makeText(MainActivity.this, "娱乐", Toast.LENGTH_SHORT).show();

                }else if (positions.equals("图书")){
                    zhye=6+"";
                    Toast.makeText(MainActivity.this, "图书", Toast.LENGTH_SHORT).show();

                }
                parent.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView parent) {
                parent.setVisibility(View.VISIBLE);
            }
        });

    }
}

你可能感兴趣的:(安卓中使用下拉框(Spinner))