SeekBar / EditText交互出错

时间:2016-06-30 14:20:13

标签: android android-edittext android-seekbar

我在尝试使用Seekbar调整EditText时遇到问题,反之亦然。 当我以最小值= 0开始我的搜索栏时,它的工作完美。 但是当我改变最小值时,它不起作用!有人可以帮帮我吗?

private void bindElements(){
    final int SEEKBAR_SIZE_CORRECTION = 200;
    final int SEEKBAR_MAX_SIZE = 1000;
    // 10000 Change to object max value
    int correctMax = SEEKBAR_MAX_SIZE - SEEKBAR_SIZE_CORRECTION;


    mEtValue = (EditText) findViewById(R.id.et_value);
    mEtValue.setFilters(new InputFilter[] { new InputFilter.LengthFilter(SEEKBAR_MAX_SIZE) });
    mEtValue.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) {
            //3 is the length of SEEKBAR_SIZE_CORRECTION (2 0 0)
            if (s.toString().length() == 3) {
                //200 is the text of SEEKBAR_SIZE_CORRECTION
                s = new SpannableStringBuilder("200");
            }
            mSbValue.setProgress(Integer.parseInt(String.valueOf(s)));
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });[![enter image description here][1]][1]

    mSbValue = (SeekBar) findViewById(R.id.sb_value);
    mSbValue.setMax(correctMax);
    mSbValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress += SEEKBAR_SIZE_CORRECTION;
            if (mEtValue != null) {
                mEtValue.setText(String.valueOf(progress));
                mEtValue.setSelection(mEtValue.getText().length());
            }
        }
    });
}

谢谢!

1 个答案:

答案 0 :(得分:0)

更新了适用于Seekbar拖动的代码。

尝试

private void bindElements(){

    final int SEEKBAR_SIZE_CORRECTION = 200;
    final int SEEKBAR_MAX_SIZE = 1000;
    // 10000 Change to object max value
    int correctMax = SEEKBAR_MAX_SIZE - SEEKBAR_SIZE_CORRECTION;


    mEtValue = (EditText) findViewById(R.id.et_value);
    mEtValue.setFilters(new InputFilter[] { new InputFilter.LengthFilter(SEEKBAR_MAX_SIZE) });
    mEtValue.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) {
            try {
                if(s.length() == 4) {
                    if(Integer.parseInt(s.toString()) > SEEKBAR_MAX_SIZE) {
                        s = s.subSequence(0,2);
                        changeText(s);
                    }
                } else if(s.length() == 3) {
                    if(Integer.parseInt(s.toString()) < SEEKBAR_SIZE_CORRECTION) {
                        s = "" + (SEEKBAR_SIZE_CORRECTION + Integer.parseInt(s.toString()));
                        changeText(s);
                    }
                } else if(s.length() < 3 && s.length() > 0) {
                    s = "" + (SEEKBAR_SIZE_CORRECTION+ Integer.parseInt(s.toString()));
                }

                if(s.length() != 0)
                    mSbValue.setProgress(Integer.parseInt(String.valueOf(Integer.parseInt(s.toString()) - SEEKBAR_SIZE_CORRECTION)));
                else
                    mSbValue.setProgress(0);
            } catch (NumberFormatException e) {

            }

        }

        @Override
        public void afterTextChanged(Editable s) {
        }
        void changeText(CharSequence s) {
            mEtValue.setText(s);
            mEtValue.setSelection(s.length());
        }
    });

    mSbValue = (SeekBar) findViewById(R.id.sb_value);
    mSbValue.setMax(correctMax);
    mSbValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (fromUser) {
                mEtValue.setText(String.valueOf(progress + SEEKBAR_SIZE_CORRECTION));
                mEtValue.setSelection(mEtValue.getText().length());
            }
        }
    });
}