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

时间:2018-03-15 12:07:29

标签: android seekbar

我希望搜索栏进度与用户输入一起使用,例如范围在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) {

        }
    });


}

User Input

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" />

2 个答案:

答案 0 :(得分:2)

TextWatcher添加到Edittext并相应地设置进度。

 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 numberandroid:inputType="number。将Edittext添加到afterTextChanged() 注意: - 这将宽松地设置进度(例如:50-> 50%,10-> 10%)。如果您想显示某些单位的进度,例如数字10-> 1%然后你需要在import java.util.Scanner; public class New { public static void main(String[] args) { System.out.println("Enter 1 to create folder/ 2 to create file/ 3 to exit"); Scanner scan= new Scanner(System.in); int choice= scan.nextInt(); while(true) { if(choice==1) { System.out.println("Creating folder"); } else if(choice==2) { System.out.println("Creating file"); } else { System.out.println("Exiting"); } } } }

中计算它

答案 1 :(得分:1)

试试这个

为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)
    {

    }
});