Android 搜索栏中的小数值

Decimal values in seek bar in Android

我正在开发 android 应用程序,我在其中使用了搜索栏。其中我的范围是 0 - 20,起始值为 5。对于数值,它工作正常,但现在我想将它设为十进制值,但范围应该是从 0.0 到 20.0。我的数字代码如下:

SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);
        seek.setProgress(5);
        seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
        textView.setText( progress + " km");// + seekBar.getMax());
        distance = progress;
        }
        });

XML 搜索栏:

<TextView
            android:id="@+id/txtSeekBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:text="5 km"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/gray" />

我已将此示例代码用于十进制值,但它不起作用。

SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);
        seek.setProgress(5);


        seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {

        double value = ((float)progress / 20.0);
        String yourprogress = String.valueOf(value);
        textView.setText( yourprogress + " km");// + seekBar.getMax());
        distance = (int) value;
        }
        });

你应该让你的进度范围从 0 到 200,然后当它改变时将当前进度乘以 0.1f。干杯。

SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);
        seek.setProgress(5 * 10);


        seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {

        float currentProgress = progress * 0.1f;
        String yourprogress = String.format("%.1f", currentProgress);
        textView.setText( yourprogress + " km");// + seekBar.getMax());
        }
        });

在您的 XML 布局中将其添加到进度条中

android:max="200"

想getProgress的时候也乘以0.1f

float currentProgress = seekBar.getProgress() * 0.1f;