Android Spinner控件详解

 

Spinner 类图

android.widget 
类 Spinner
java.lang.Object
  android.view.View
      android.view.ViewGroup
          android.widget.AdapterView<SpinnerAdapter>
              android.widget.AbsSpinner
                  android.widget.Spinner

 

Spinner 意指下拉列表组件。

 

以下为android官网文档内容。

 

布局文件中加入Spinner元素标签

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

To populate the spinner with a list of choices, you then need to specify a SpinnerAdapter in your Activity orFragment source code.

为了给spinner增加选择项,你需要在你的Activity或者Fragment中使用一个特定的SpinnerAdapter对象作为数据源绑定到Spinner对象上。

 

The choices you provide for the spinner can come from any source, but must be provided through anSpinnerAdapter, such as an ArrayAdapter if the choices are available in an array or a CursorAdapter if the choices are available from a database query.

虽然你可以给spinner提供任一数据源,但是这种数据源必须是实现SpinnerAdapter接口的。例如ArrayAdapter。如果选择从数据库中查询得到数据,那么你可以使用CursorAdapter这种Adapter作为数据源。

 

可以使用string数组。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
    </string-array>
</resources>

With an array such as this one, you can use the following code in your Activity or Fragment to supply the spinner with the array using an instance of ArrayAdapter:

如果使用string数组,你需要在你的Activity或者Fragment中使用ArrayAdapter。

Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

静态方法 createFromResource(Context context, int textArrayResId, int textViewResId )

参数context       这里使用this 意指Activity

参数textArrayResId   这里使用R.array.plants_array  数据来源

参数textViewResId    这里使用android.R.layout.simple_spinner_item(android提供 也可以自己定制) Spinner的布局样式

 

You should then call setDropDownViewResource(int) to specify the layout the adapter should use to display the list of spinner choices (simple_spinner_dropdown_item is another standard layout defined by the platform).

可以调用 setDropDownViewResource(int) 去指定spinner列表中选项的布局和样式。

 

如何响应用户的选择,实现OnItemSelectedListener接口

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
    ...
    
    public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

 

The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected()callback methods.

其中此接口需要实现其中的两个方法

 

Then you need to specify the interface implementation by calling setOnItemSelectedListener():(指定spinner对象实现OnItemSelectedListener接口)

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);

 

你可能感兴趣的:(android)