问题
I have implemented the following library spinner in my app i.e from xml
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="@+id/insurer_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_insurer_code"
android:textColor="@color/smart_primary"
android:textColorHint="@color/input_register_hint"
app:met_floatingLabel="normal" />
and java code
public class testActivity extends Activity implements OnItemSelectedListener
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(adapterView.getContext(), "Selected: " , Toast.LENGTH_LONG).show();
// On selecting a spinner item
String item = adapterView.getItemAtPosition(i).toString();
// Showing selected spinner item
Toast.makeText(adapterView.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
but the onItemSelected doesn't fire up when an item is selected from the menu. Any guidance on how to successfully implement the above library will be appreciated.
回答1:
just simple add this one and works like a charm!! `
materialDesignSpinner.setAdapter(arrayAdapter);
materialDesignSpinner.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) {
quantity=materialDesignSpinner.getText().toString();
Log.d("value",quantity);
}
});
`
回答2:
The setOnItemSelectedListener does not work, because the Material/BetterSpinner is a EditText with auto-complete. Not a really Spinner. you can see the answer here: https://github.com/Lesilva/BetterSpinner/issues/42
回答3:
You can use this code.
MaterialBetterSpinner MBS = (MaterialBetterSpinner) findViewById(R.id.Spinner);
MBS.addTextChangedListener(new myTextWatcher() {
@Override
public void afterTextChanged(Editable s) {
Log.e("Text",MBS.getText().toString());
}
});
Create a myTextWatcher class then copy paste the code below.
import android.text.Editable;
import android.text.TextWatcher;
public class myTextWatcher implements TextWatcher {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void afterTextChanged(Editable s) {}
}
回答4:
Use this
public class DropDownList extends MaterialBetterSpinner {
private AdapterView.OnItemSelectedListener listener;
public DropDownList(Context context) {
super(context);
}
public DropDownList(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public DropDownList(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
super.onItemClick(adapterView, view, i, l);
if (listener != null)
listener.onItemSelected(adapterView, view, i, l);
}
@Override
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener l) {
super.setOnItemSelectedListener(l);
listener = l;
}
}
来源:https://stackoverflow.com/questions/36823449/how-can-i-implement-onitemselected-on-materialbetterspinner-library