Spinner简单使用

适配器:SpinnerUtil类:

public class SpinnerUtil{
    private Spinner spinner;
    private Context context;
    private List<SinnerUtilVo> list = new ArrayList<SinnerUtilVo>();
    public SpinnerUtil(Context context,Spinner spinner) {
        this.context = context;
        this.spinner = spinner;
    }

    public void load(){
        ArrayAdapter<SinnerUtilVo> adapter = new ArrayAdapter<SinnerUtilVo>(context,android.R.layout.simple_spinner_dropdown_item,list);
        this.spinner.setAdapter(adapter);
    }

    public String getValue(){
        Object object = this.spinner.getSelectedItem();
        SinnerUtilVo picksortitem = (SinnerUtilVo) object;
        return picksortitem.getValue();
    }

    public void setKeyValue(String key,String value){
        list.add(new SinnerUtilVo(key, value));
    }
}

注:android.R.layout.simple_spinner_dropdown_item可更换样式

辅助类:SinnerUtilVo

public class SinnerUtilVo {
    private String key;
    private String value;

    @Override
    public String toString(){
        return this.key;
    }
    public SinnerUtilVo(String key,String value){
        this.key = key;
        this.value = value;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

简单使用:
private Spinner mattypeSpinner;
private SpinnerUtil spinnerUtil;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {

        mattypeSpinner = (Spinner) findViewById(R.id.mattypeSpinner);
        spinnerUtil = new SpinnerUtil(this,mattypeSpinner); 
        //spinnerUtil.setKeyValue("是否赠送物料", "");
        spinnerUtil.setKeyValue("否", "0");
        spinnerUtil.setKeyValue("是", "1");
        spinnerUtil.load();
        mattypeSpinner.setOnItemSelectedListener(new OnItemSelectedListenerImpl());


            }
    } catch (Exception e) {
        // TODO: handle exception
    }
}

注:OnItemSelectedListenerImpl()自定义(选择操作),例如:

    private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view,int position, long id) {
            TextView tv=(TextView)view;
            tv.setTextSize(20.0f);
            Object object = mattypeSpinner.getItemAtPosition(position);
            SinnerUtilVo sinnerUtilVo = (SinnerUtilVo) object;
            String prentFlag=sinnerUtilVo.getValue();
            presentflag.setText(prentFlag);
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    }

设置下拉框初始默认值,可简单设置如下:
Spinnerd对象.setSelection(int index,boolean f);

阅读更多
文章标签: Android
个人分类: android
上一篇ListView 中嵌套了OnClickListener事件,而本身onItemClick事件无响应的解决方案
下一篇Activity跳转与传值(传递与回写)
想对作者说点什么? 我来说一句

Android spinner

2013年04月15日 70KB 下载

spinner简单使用

2015年11月02日 3.28MB 下载

Spinner简单使用

Spinner

lmf0421 lmf0421

2016-03-24 11:41:01

阅读数:246

没有更多推荐了,返回首页

关闭
关闭