Android循环监听器

时间:2014-11-28 09:24:20

标签: 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)运作良好,但不是第二种情况。 问题是它是一个循环(SeekBarEditTextSeekBarEditText等。)

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:1)

我建议使用两个布尔参数来锁定更改,如下所示:

    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)
        {
        }
    });

我没有测试过,但它应该有用。