欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android的spinner获取索引和设置显示第几项

程序员文章站 2022-06-21 19:10:39
//选择业务类型的下拉 Spinner mSpinner; //业务类型下拉 void businessTypeSelect() { String[] arr = {"门店要货", "门店退货", "店间调货"}; ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arr) { ....
 //选择业务类型的下拉
    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) {

            }
        });
    }

本文地址:https://blog.csdn.net/ShiXinXin_Harbour/article/details/108711561

相关标签: android web app