Android Spinner 动态增加 item,以及添加 Spinner 监听选择/选中项事件 作者: Chuwen 时间: 2020-10-28 分类: Android ``` @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.confrim_activity); //获取控件 Spinner startPlace = (Spinner) findViewById(R.id.start_place); List stringList = new ArrayList<>(); stringList.add("项目1"); stringList.add("项目2"); stringList.add("项目3"); //使用系统的默认样式 android.R.layout.simple_spinner_item ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stringList); //使用系统默认下拉样式 android.R.layout.simple_spinner_dropdown_item adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); startPlace.setAdapter(adapter); //监听选择事件 startPlace.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> parent, View view, int position, long id) { //TODO 选中事件 //position:选中的哪个 stringList.get(position) } @Override public void onNothingSelected(AdapterView> parent) { } }); } ``` ![QQ20201028-105957@2x.png][1] [1]: https://cdn.nowtime.cc/2020/10/28/1239940170.png 标签: Android, Android 笔记