//选择业务类型的下拉
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() {
@Override
public 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);//显示第一项来显示
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Android的spinner获取索引和设置显示第几项
最新推荐文章于 2022-10-09 16:19:44 发布