Android:下拉列表Spinner

l Spinner.getItemAtPosition ( Spinner.getSelectedItemPosition ()); 获取下拉列表框的值
l 调用 setOnItemSelectedListener () 方法,处理下拉列表框被选择事件,把 AdapterView.OnItemSelectedListener 实例作为参数传入


界面设计:

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="fill_parent"

 android:layout_height="wrap_content">

  <Spinner   android:id="@+id/spinner"

   android:layout_height="wrap_content"

   android:layout_width="fill_parent"/>

</LinearLayout>

代码处理:

public class SpinnerActivity extends Activity {

    private staticfinal String TAG = "SpinnerActivity";

    @Override

    public voidonCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.spinner);

        //第二个参数为下拉列表框每一项的界面样式,该界面样式由Android系统提供,当然您也可以自定义

        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);

       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

       adapter.add("java");

       adapter.add("dotNet");

       adapter.add("php");

        Spinnerspinner = (Spinner) findViewById(R.id.spinner);

       spinner.setAdapter(adapter);

       spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {

  @Override

  public voidonItemSelected(AdapterView<?> adapterView, View view, int position, longid) {

  Spinner spinner =(Spinner)adapterView;

  StringitemContent = (String)adapterView.getItemAtPosition(position);

  }

  @Override

  public voidonNothingSelected(AdapterView<?> view) {

  Log.i(TAG,  view.getClass().getName());

  }

        });

    }

}


你可能感兴趣的:(Android:下拉列表Spinner)