Android中搜索栏中的十进制值

时间:2015-04-29 06:28:08

标签: android decimal seekbar android-seekbar

我正在使用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;
        }
        });

1 个答案:

答案 0 :(得分:4)

您应该使进度范围从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;