SPinner 组件

android

2013-04-27 16:04

先加入   

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
 />


然后 layout 新建  xl.xml


放入 TextView 


<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textAppearance="?android:attr/textAppearanceLarge" />


ArrayAdapter<String> adapter;
sp=(Spinner)findViewById(R.id.spinner1);  //获取 Spinner

 ArrayList ls=new ArrayList();

ls.add("第1个");
ls.add("第2个");
ls.add("第3个");

    adapter = new ArrayAdapter<String>(this,  android.R.layout.simple_spinner_item, ls);  建立适配器 
    adapter.setDropDownViewResource(R.layout.xl);                                         设置布局
    sp.setAdapter(adapter);                                                                          绑定适配器


sp.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
                
                String seleted = asd.get(position).toString();    //获取值
                ll=position;                                               //获取选择ID
                tv.setText(seleted);                                    显示
                //parent.setVisibility(View.VISIBLE);                   
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                tv.setText("您没有选择");
            }
        });



--------------------------------------------------------------------------------------------------------------

    zj.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                String asdasd=ed.getText().toString();
                if (asdasd.length()==0){
                     Toast.makeText(getBaseContext(), "内容不能为空。", 3).show();
                }
                else{
                ls.add(asdasd);
                }
                            
                
            }
    });



----------------------------------------------------------------------------------------------------------------------


    qd.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                ls.remove(ll);
                Toast.makeText(getBaseContext(), “删除成功”, 3).show();
                
                
            }
    });


----------------------------------------------------------------------------------------------------------------------

Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(), 5).show();

直接显示选择后的值



另外andorid也提供了两种基本的样式
android.R.layout.simple_spinner_item:TextView的下拉菜单
android.R.layout.simple_spinner_dropdown_item:右边带有radio的下拉菜单