本文主要是介绍Android的spinner获取索引和设置显示第几项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//选择业务类型的下拉Spinner mSpinner;//业务类型下拉void businessTypeSelect() {String[] arr = {"门店要货", "门店退货", "店间调货"};ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr) {};mSpinner = findViewById(R.id.spinner_businessType_allocationList);mSpinner.setAdapter(adapter);mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {//获取下拉的索引if (mSpinner.getSelectedItemPosition() == 0) {Toast.makeText(getBaseContext(), "你选中的选项为" + mSpinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();} else if (mSpinner.getSelectedItemPosition() == 1) {Toast.makeText(getBaseContext(), "你选中的选项为" + mSpinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();} else if (mSpinner.getSelectedItemPosition() == 2) {Toast.makeText(getBaseContext(), "你选中的选项为" + mSpinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();}
//设置显示的的某一项来显示
mSpinner.setSelection(2);//显示第三项来显示mSpinner.setSelection(0);//显示第二项来显示mSpinner.setSelection(1);//显示第一项来显示}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});}
这篇关于Android的spinner获取索引和设置显示第几项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!