Android常用控件——下拉列表的设置

实现Android下拉列表的方法

1、首先在layout中设置控件Spinner

    

2、在Activity中声明变量mSpinner

private Spinner mSpinner;

3、在Activity中通过id引用控件

mSpinner = (Spinner) findViewById(R.id.spinner);

4、通过setAdapter()方法设置下拉列表

        mSpinner.setAdapter(new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item,
                datasource));

其中datasouece是字符串数组,比如可以定义为

    private String datasource[] = new String[]{"1111","2222","3333","4444",
    "5555","6666","7777","8888","9999","0000"};

5、获取用户的选择

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
                System.out.println("用户选择的是:"+datasource[i]);
            }

            @Override
            public void onNothingSelected(AdapterView adapterView) {

            }
        });
即通过i来获知字符串数组的下标

 
  
 
  

你可能感兴趣的:(Android,android,控件)