我正在使用MaterialBetterSpinner下拉列表,当用户从该下拉列表中选择值时,我想获取价值。我已经在许多网站中搜索过,但是我找不到任何解决方案。有什么办法可以从这个微调器中获取价值,

我的代码

MaterialBetterSpinner materialDesignSpinner = (MaterialBetterSpinner)
                v.findViewById(R.id.android_material_design_spinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, SPINNERLIST);
materialDesignSpinner.setAdapter(arrayAdapter);
materialDesignSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      String  selected=String.valueOf(materialDesignSpinner.getText());
        Toast.makeText(getActivity(), selected, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});


XML代码

<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:id="@+id/android_material_design_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Blood Group Type"
    android:textColorHint="#05ab9a"
    app:met_floatingLabel="normal" />

最佳答案

materialDesignSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String spinner_value= adapterView.getItemAtPosition(position).toString();
                Toast.makeText(getApplicationContext(),spinner_value,Toast.LENGTH_LONG).show();
            }
        });

关于android - 如何从MaterialBetterSpinner中选择值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46047464/

10-08 21:52