Java 从 android 中的 SeekBar 获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19227722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:15:07  来源:igfitidea点击:

Getting values from SeekBar in android

javaandroid

提问by

How do I get values from a SeekBar?

我如何从 a 中获取值SeekBar

  • I have a code of a class that has three SeekBars (PRICEbar)
  • I want to pass the values of these SeekBars to next Activity (screen) as Intents
  • I know how toimplement OnClickListener but how can I extract the values from the SeekBars?
  • 我有一个包含三个 SeekBars ( PRICEbar)的类的代码
  • 我想将这些 SeekBars 的值作为 Intent 传递给下一个 Activity(屏幕)
  • 我知道如何实现 OnClickListener 但如何从 SeekBars 中提取值?

ex:if PRICEbaris pointing to value 10 ... I need to get the value of 10

例如:如果PRICEbar指向值 10 ...我需要获得 10 的值

Filters.java

过滤器.java

public class Filters extends Activity implements OnSeekBarChangeListener{

    // declare text objects variables
    private SeekBar PRICEbar; 
    private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;
    Button back;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.filters);   

        PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
        PRICEbar.setOnSeekBarChangeListener(this); 
        PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

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

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
                PRICEtextProgress.setText("Price:: Rs "+progress);
                seekBar.setMax(100);
            }
        });
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar == PRICEbar)
            PRICEtextProgress.setText("Price:: Rs "+progress);
    }

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

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }
}

采纳答案by Bryan Herbst

The SeekBarclass is a subclass of ProgressBar, which contains a getProgress()method.

SeekBar类是的一个子类ProgressBar,其中包含一个getProgress()方法。

Calling PRICEbar.getProgress()will return the value you are looking for.

调用PRICEbar.getProgress()将返回您要查找的值。