从android中获取SeekBar的值

时间:2013-10-07 14:40:53

标签: java android

如何从SeekBar获取值?

  • 我有一个包含三个SeekBars(PRICEbar
  • 的类的代码
  • 我想将这些SeekBars的值传递给下一个Activity(屏幕) 作为意图
  • 我知道如何实现OnClickListener但是如何提取 来自SeekBars的价值?

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

Filters.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
    }
}

1 个答案:

答案 0 :(得分:25)

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

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