根据EditText值更改SeekBar进度

时间:2013-12-27 14:29:56

标签: java android android-edittext seekbar

我正在尝试根据SeekBar中输入的数字更改EditText的进度但由于某种原因,EditText值达到最大值而我无法滑动拇指在SeekBar

我希望实现的目标:如果在EditText中输入的值在70到190之间(包括两个数字),则会将SeekBar的进度更改为该值。

部分Java代码:

etOne = (EditText) findViewById(R.id.etSyst);
        etOne.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                String filtered_str = s.toString();
                if (Integer.parseInt(filtered_str) >= 70 && Integer.parseInt(filtered_str) <= 190) {
                    sbSyst.setProgress(Integer.parseInt(filtered_str));
                }
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

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

部分XML:

<SeekBar
            android:id="@+id/syst_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:progress="0"
            android:max="120"
            android:progressDrawable="@drawable/progress_bar"
            android:secondaryProgress="0"
            android:thumb="@drawable/thumb_state" />

我为每个值添加70,因为SeekBar从0开始,但我希望它从70开始。

使用上面的代码后,它就是这样的:

enter image description here

SeekBar是最大数量,EditText也是最大数量。

1 个答案:

答案 0 :(得分:1)

etOne = (EditText) findViewById(R.id.etSyst);
        etOne.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                int i = Integer.parseInt(s.toString());
                if (i >= 70 && i <= 190) {
                    sbSyst.setProgress( i - 70); // This ensures 0-120 value for seekbar
                }
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            public void onTextChanged(CharSequence s, int start, int before, int count) {}
        });
相关问题