关闭

Spinner简单使用

标签: Android
233人阅读 评论(0) 收藏 举报
分类:

适配器: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);

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21016次
    • 积分:560
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:16篇
    • 译文:0篇
    • 评论:0条