对于手机和平板电脑的应用程序来说,打字是非常不方便的操作方式,比较好的方式就是列出一组选项让用户挑选,这样就可以避免打字的麻烦。使用Spinner下拉菜单组件需要完成以下几个步骤:

  1.建立选项列表,选项列表中包含许多项目名称,这些项目名称是用数组的方式代表;

  2.把选项列表设置给一个Spinner接口组件;

  3.设置Spinner组件的菜单显示格式;

  4.设置Spinner组件的OnItemSelectedListener()事件处理程序,当用户单击某个项目之后,程序必须取得该项目所对应的数据。

  特别提示:建立选项列表有两种方式,第一种是直接将选项列表以数组的方式宣告在程序中。这种方式比较简单,但是我们在第五章提到过MVC设计模式,里面提到过应该尽量将程序代码与文字等数据分开,所以就有了第二种选项列表建立方式。我们把项目列表建立在项目的strings.xml文件中,在让程序从项目的资源类R中取得选项列表数组。

 

  我们可以自己定义一个菜单格式定义文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http:///apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    />

  main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:///apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/promptSex"/>
    <Spinner
        android:id="@+id/spnSex"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:drawSelectorOnTop="true"
        android:prompt="@string/spnSexPrompt"/>
        android:spinnerMode="dialog"/>
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/promptAge"/>
    <EditText
        android:id="@+id/edtAge"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:inputType="number"
        android:text=""/>
    <Button
        android:id="@+id/btnDoSug"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/promptBtnDoSug"/>
    <TextView
        android:id="@+id/txtResult"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/sugResult"/>
</LinearLayout>

strings.xml文件:
<resources>
    <string name="app_name">健身咨询</string>
    <string name="promptSex">性别:</string>
    <string name="spnSexPrompt">性别:</string>
    <string name="promptAge">年龄:</string>
    <string name="promptBtnDoSug">健身咨询</string>
    <string name="sugResult">结果:</string>
    <string name="sugRun">跑步</string>
    <string name="sugSwim">游泳</string>
    <string name="sugSuggestion">健康咨询</string>
    <string name="sexMale">男</string>
    <string-array name="spnSexList">
        <item>男</item>
        <item>女</item>
    </string-array>
</resources>

程序代码:
public class MainActivity extends Activity
{

    private Button btnDoSug;
    private EditText edtAge;
    private TextView txtResult;
    private Spinner spnSex;

    private String sSex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupViewComponent();
    }

    private void setupViewComponent()
    {
        //从资源类R中取得接口组件
        btnDoSug = (Button)findViewById(.btnDoSug);
        spnSex = (Spinner)findViewById(.spnSex);
        edtAge = (EditText)findViewById(.edtAge);
        txtResult = (TextView)findViewById(.txtResult);

        ArrayAdapter<CharSequence> adapSexList = ArrayAdapter.createFromResource(
                this, R.array.spnSexList, R.layout.spinner_layout);
        spnSex.setAdapter(adapSexList);
        spnSex.setOnItemSelectedListener(spnSexItemSelLis);

        //button组件事件的listener
        btnDoSug.setOnClickListener(btnDoSugOnClick);
    }

    private Spinner.OnItemSelectedListener spnSexItemSelLis = new Spinner.OnItemSelectedListener()
    {
        public void onItemSelected(AdapterView parent, View v, int position, long id)
        {
            sSex = parent.getSelectedItem().toString();
        }
        public void onNothingSelected(AdapterView parent)
        {
            //null
        }
    };

    private Button.OnClickListener btnDoSugOnClick = new Button.OnClickListener()
    {

        public void onClick(View view){

            int iAge = Integer.parseInt(edtAge.getText().toString());

            String strSug = "结果:";
            if(sSex.equals("男"))
            {
                if(iAge < 28)
                    strSug += getString(R.string.sugRun);
                else if(iAge > 33)
                        strSug += getString(R.string.sugRun);
                    else
                    strSug += getString(R.string.sugRun);
            }
            else
            {
                if(iAge < 28)
                    strSug += getString(R.string.sugRun);
                else if(iAge > 33)
                    strSug += getString(R.string.sugSwim);
                else
                    strSug += getString(R.string.sugSwim);
            }

            txtResult.setText(strSug);
        }
    };
}
效果截图:

android 下拉listview阴影 android下拉列表组件_android