Android Seekbar 通过用户输入设置进度

Android Seekbar set progress by user input

我希望搜索栏进度与用户输入一起工作,例如范围在 1-100 之间,如果我输入 50,拇指应该在中间。任何帮助将不胜感激。提前致谢。

ColorSlider.java

public class ColorSlider extends AppCompatActivity {

private SeekBar seekBar = null;
private View view;
private TextView progress;
private ShapeDrawable shape;
private EditText userInput;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_slider);

    seekBar = (SeekBar)findViewById(R.id.seekBar);
    progress = (TextView)findViewById(R.id.progress);
    userInput = (EditText)findViewById(R.id.userInput);

    LinearGradient test = new LinearGradient(0.f, 0.f, 320.f, 320.f, new int[] { 0xFF0000FF, 0xFF00FF00, 0xFFFF0000},
            null, Shader.TileMode.CLAMP);

    shape = new ShapeDrawable(new RectShape());
    shape.getPaint().setShader(test);

    seekBar.setProgressDrawable((Drawable) shape);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            progress.setText(" "+i);

        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


}

activity_color_slider.xml

<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_weight="1"
    android:max="100"
    android:progress="0"
    android:progressDrawable="@drawable/progress_drawable"
    android:splitTrack="false"
    android:thumb="@drawable/drag_thumb"
    tools:ignore="MissingConstraints" />

添加一个TextWatcherEdittext并相应地设置进度。

 et1.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String text = editable.toString();
            if(!TextUtils.isEmpty(text)){
                seekBar.setProgress(Integer.parseInt(text));
            }
        }
    });

确保 Edittext 应该有 inputType 作为 number 。将 android:inputType="number 添加到 Edittext.
注意:- 这将宽松地设置进度(例如:50->50%,10->10%)。如果你想用数字 10-> 1% 等单位显示进度,那么你需要在 afterTextChanged()

内计算它

试试这个

并为 Edittext 实施 textwatcher

  youredittext.addTextChangedListener(new TextWatcher()
{
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
               seekbar.setProgress(Integer.parseInt(s));
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int aft )                                                                          
    {

    }

    @Override
    public void afterTextChanged(Editable s)
    {

    }
});