我有一个SeekBar和EditText。我想创建一个%滑块,当用户滑动Seekbar时,它会调整EditText字段。
我试过这个:
mInterestRateEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = Integer.parseInt(s.toString());
if (i >= 0 && i <= 100) {
mInterestRateSeekBar.setProgress(50);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
mInterestRateSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mInterestRateEditText.setText(progress + "");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
但它所做的只是第二次尝试滑动滑块,它将EditText锁定为0,我无法移动滑块?
答案 0 :(得分:1)
正如我在评论中所说,你正在创建一个循环。更改Seekbar,更改文本。这会再次改变搜索栏。
简单的解决方案:使用布尔值。
boolean changedProgrammatically;
mInterestRateEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = Integer.parseInt(s.toString());
if(!changedProgrammatically){
if (i >= 0 && i <= 100) {
mInterestRateSeekBar.setProgress(50);
}
}else{
changedProgrammatically = false;
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
mInterestRateSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
changedProgrammatically = true;
mInterestRateEditText.setText(progress + "");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});