问题
Here i use a value changing effect on a slider; if i move the slider, it will scroll and display updated values in an edit field, as per the moving of the slider bar. Also, there is an edit field effect where the slider should be moved as per the values entered into the edit field; but it is not working.
When i comment out part of the edit field effect, it is working properly but when i apply that edit field effect again, then it is not working ...
// for slider move
FieldChangeListener listenerslider1 = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
try {
if (field == serumosmslider) {
int serumosmslidervalue = serumosmslider.getValue();
String strplasmaslidervalue = Integer
.toString(serumosmslidervalue);
edtserumosm.setText(strplasmaslidervalue);
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
};
serumosmslider.setChangeListener(listenerslider1);
// for editfield
FieldChangeListener listenereditslider1 = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
hfmslider1.deleteAll();
String stredtweight = edtserumosm.getText().toString();
int editweight = Integer.parseInt(stredtweight);
SliderField theSlider = new SliderField(slider2thumb,
slider2progress, slider2base, slider2thumbfoc,
slider2progressfoc, slider2basefoc, 201,
editweight, 10, 10);
hfmslider1.add(theSlider);
hfmslider1.invalidate();
}
};
edtserumosm.setChangeListener(listenereditslider1);
回答1:
You appear to be recreating your SliderField
every time the EditField
's value changes.
SliderField theSlider = new SliderField(slider2thumb,
slider2progress, slider2base, slider2thumbfoc,
slider2progressfoc, slider2basefoc, 201,
editweight, 10, 10);
hfmslider1.add(theSlider);
I don't think you want to do that. Just like you update the EditField
text when the slider field changes, I think you should update the SliderField
value when the EditField
text is changed. So, something like this:
FieldChangeListener listenereditslider1 = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (field == edtserumosm && context != FieldChangeListener.PROGRAMMATIC) {
String stredtweight = edtserumosm.getText().toString();
try {
int editweight = Integer.parseInt(stredtweight);
serumosmslider.setValue(editweight);
} catch (NumberFormatException nfe) {
// TODO: anything?
}
}
}
};
edtserumosm.setChangeListener(listenereditslider1);
来源:https://stackoverflow.com/questions/20214592/issue-while-using-moving-effect-on-slider