我正在让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" />
答案 0 :(得分:1)
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();
}
});
答案 1 :(得分:0)
MaterialBetterSpinner类中有以下方法。
public int getPosition() {
return mPosition;
}
您可以使用materialDesignSpinner.getPostion()
获取所选项目的位置,然后您可以从列表中获取该位置。
答案 2 :(得分:0)
使用.setOnItemClickListener,它适用于我。
categorySpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("kkkk",""+position);
}
}
答案 3 :(得分:0)
materialDesignSpinner_gender.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String gender=materialDesignSpinner_gender.getText().toString();
Toast.makeText(getApplicationContext(),gender ,Toast.LENGTH_LONG).show();
}
});