/ / Androidのサイクリックリスナー - android、android-edittext、android-seekbar

Androidのサイクリックリスナー - android、android-edittext、android-seekbar

私は自分のコードに小さな問題があります:

私は SeekBarEditText レイアウトで。 私がしたいのは、 EditText 値は SeekBar 変更して設定する SeekBar 進歩 EditText 変更。

これまでのところ、私は次のコードを書いています:

    SeekBar seek_coagulation = (SeekBar) findViewById(R.id.seek_coagulation);
EditText edit_coagulation = (EditText) findViewById(R.id.edit_coagulation);
seek_coagulation.incrementProgressBy(1);
seek_coagulation.setMax(1000);
seek_coagulation.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
double value = progress / 100.0;
edit_coagulation.setText(String.valueOf(value));
}

public void onStartTrackingTouch(SeekBar seekBar)
{
}

public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
edit_coagulation.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s)
{
double value = (s != null && s.toString().length() > 0) ? Double.parseDouble(s.toString().replace(",", ".")) : 0D;
int progress = (int) (value * 100);
seek_coagulation.setProgress(progress);
}

public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}

public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
});

最初のケース(SeekBarEditText)はうまくいくが、2番目のものはうまくいかない。 問題はそれがループであることですSeekBarEditTextSeekBarEditText 等。)。

それを修正する方法はありますか?

回答:

回答№1は1

次のように、2つのブール値パラメータを使用して変更をロックすることをお勧めします。

    boolean mIsTextLocked = false;
boolean mIsSeekBarLocked = false;

seek_coagulation = (SeekBar) findViewById(R.id.seek_coagulation);
EditText edit_coagulation = (EditText) findViewById(R.id.edit_coagulation);
seek_coagulation.incrementProgressBy(1);
seek_coagulation.setMax(1000);
seek_coagulation.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if(!mIsSeekBarLocked){
mIsTextLocked = true;
double value = progress / 100.0;
edit_coagulation.setText(String.valueOf(value));
}
}

public void onStartTrackingTouch(SeekBar seekBar)
{
}

public void onStopTrackingTouch(SeekBar seekBar)
{
mIsTextLocked = false;
}
});
edit_coagulation.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s)
{
if(!mIsTextLocked){
mIsSeekBarLocked = true;
double value = (s != null && s.toString().length() > 0) ?Double.parseDouble(s.toString().replace(",", ".")) : 0D;
int progress = (int) (value * 100);
seek_coagulation.setProgress(progress);
mIsSeekBarLocked = false;
}
}

public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}

public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
});

私はそれをテストしていませんが、うまくいくはずです。